Format date as yyyy-mm-dd using vbscript

Function GetFormattedDate
  strDate = CDate(Date)
  strDay = DatePart("d", strDate)
  strMonth = DatePart("m", strDate)
  strYear = DatePart("yyyy", strDate)
  If strDay < 10 Then
    strDay = "0" & strDay
  End If
  If strMonth < 10 Then
    strMonth = "0" & strMonth
  End If
  GetFormattedDate = strYear & "-" & strMonth & "-" & strDay
End Function

'For example: the date string will be 2011-03-21
GetFormattedDate()

Reference

http://www.w3schools.com/vbScript/func_formatdatetime.asp

Comments

Post a Comment

Popular posts from this blog

How to write data into a excel file using vbscript