VBScript to convert timestamp to YYYYMMDD formatted string

I started with the code on this page.

Function FormatYYYYMMDD(timeStamp)

Dim dateMonth : dateMonth = DatePart("M", timeStamp)
Dim dateDay : dateDay = DatePart("D", timeStamp)
Dim dateYear : dateYear = DatePart("YYYY", timeStamp)
Dim dateString

dateString = dateYear

If dateMonth < 10 Then
dateString = dateString & "0" & dateMonth
Else
dateString = dateString & dateMonth
End If

If dateDay < 10 Then
dateString = dateString & "0" & dateDay
Else
dateString = dateString & dateDay
End If

FormatYYYYMMDD = dateString
End Function

One Reply to “VBScript to convert timestamp to YYYYMMDD formatted string”

  1. Excellent! You've just helped me out of a hole

    Here's the code to do the same for hours, mins and seconds:

    Function FormatHMS(timestamp)

    Dim dateHour : dateHour = DatePart("h", timeStamp)
    Dim dateMinute : dateMinute = DatePart("n", timeStamp)
    Dim dateSecond : dateSecond = DatePart("s", timeStamp)

    If dateHour < 10 Then
    timeString = timeString & "0" & dateHour
    Else
    timeString = timeString & dateHour
    End If

    timeString = timeString & ":"

    If dateMinute < 10 Then
    timeString = timeString & "0" & dateMinute
    Else
    timeString = timeString & dateMinute
    End If

    timeString = timeString & ":"

    If dateSecond < 10 Then
    timeString = timeString & "0" & dateSecond
    Else
    timeString = timeString & dateSecond
    End If

    FormatHMS = timeString

    End Function

Comments are closed.