• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 630
  • Last Modified:

Format dates in ASP (VBScript)

     aspStartDate = date()
      response.write("Start Date" & aspStartDate & "<br>")

This is my code:
It shows up as:     5/22/2006

I would like it to show up in this format:  '2006-5-22'


 
0
Gemini532
Asked:
Gemini532
4 Solutions
 
_Maddog_Commented:
Hi Gemini532,

Here is a nice resource on date formatting in VBScript:
http://www.ilovejackdaniels.com/asp/vbscript-date-format-functions/

- Maddog
0
 
kevp75Commented:
I use this:
'Format Date
'Usage FormatDate("switch(es) to use", DateToFormat)
' "a" = am or pm
' "A" = AM or PM
' "B" = Swatch Internet Time
' "d" = day of month with leading 0
' "D" = day of week, 3 letter
' "F" = long month name
' "g" = hour, 12-hour non-leading 0 format
' "G" = hour, 24-hour non-leading 0 format
' "h" = hour, 12-hour leading 0 format
' "H" = hour, 24-hour leading 0 format
' "i" = minutes
' "j" = day of month, non-leading 0
' "l" = long day name
' "L" = boolean leap year, 0 = no, 1 = yes
' "m" = month number leading 0
' "M" = 3 letter month name
' "n" = month number non-leading 0
' "r" = RFC 822 formatted date/time
' "s" = seconds
' "S" = th, nd, rd, etc...
' "t" = number of days in a given month
' "U" = UNIX timestamp
' "w" = numeric day of week
' "W" = ISO-8601 week number of the year
' "Y" = long year
' "y" = short year
' "z" = day of year
Private Function InternetTime()
      Dim lLngTime
      Dim lLngBeats
      Dim lLngBeatsRound
      lLngTime = (Hour(DateToFormat) * 3600 * 1000) + (Minute(DateToFormat) * 60 * 1000) + (Second(DateToFormat) * 1000 + 3600000)
      lLngBeats = lLngTime / 86400
      lLngBeatsRound = Round(lLngBeats)
      InternetTime = "@" & lLngBeatsRound
      If lLngBeatsRound > 1000 Then InternetTime = "@0" & lLngBeatsRound
      If lLngBeatsRound > 100 Then InternetTime = "@0" & lLngBeatsRound
End Function ' InternetTime()
Private Function LeadingZero(ByRef pStrValue)
      If Len(pStrValue) < 2 Then pStrValue = "0" & pStrValue
      LeadingZero = pStrValue
End Function ' LeadingZero(ByRef pStrValue)
Private Function IsLeap()
      Dim lLngYear
      lLngYear = Year(DateToFormat)
      IsLeap = 0
      If (lLngYear Mod 4 = 0) And (lLngYear Mod 100 <> 0) Or (lLngYear Mod 400 = 0) Then IsLeap = 1
End Function ' IsLeap()
Private Function DaysInMonth()
      Dim lLngMonth
      lLngMonth = Month(DateToFormat)
      Select Case lLngMonth
            Case 9, 4, 6, 11 : DaysInMonth = 30
            Case 2 : If CBool(IsLeap()) Then DaysInMonth = 29 Else DaysInMonth = 28
            Case Else : DaysInMonth = 31
      End Select
End Function ' DaysInMonth()
Private Function FormatHour()
      Dim lDtmNow
      lDtmNow = FormatDateTime(DateToFormat,3)
      FormatHour = Left(lDtmNow,InStr(lDtmNow,":") - 1)
End Function ' FormatHour()
Private Function OrdinalSuffix()
      Dim lLngDay
      lLngDay = Day(DateToFormat)
      OrdinalSuffix = "th"
      If lLngDay = 1 Then OrdinalSuffix = "st"
      If lLngDay = 2 Then OrdinalSuffix = "nd"
      If lLngDay = 3 Then OrdinalSuffix = "rd"
End Function ' OrdinalSuffix()
Public Function FormatDate(ByRef pStrDate, DateToFormat)
      ' Define local variables
      Dim lObjRegExp
      Dim lObjRegMatches
      Dim lObjMatch
      Dim lDtmNow
      Dim lLngSecond
      Dim lLngMinute
      Dim lLngHour
      Dim lLngDay
      Dim lLngWeekDay
      Dim lLngMonth
      Dim lLngYear
      lDtmNow = DateToFormat
      lLngSecond = Second(lDtmNow)
      lLngMinute = Minute(lDtmNow)
      lLngHour = Hour(lDtmNow)
      lLngDay = Day(lDtmNow)
      lLngWeekDay = WeekDay(lDtmNow)
      lLngMonth = Month(lDtmNow)
      lLngYear = Year(lDtmNow)
      ' Prepare RegExp object and set parameters
      Set lObjRegExp = New RegExp
      lObjRegExp.Pattern = "([a-z][a-z]*[a-z])*[a-z]"
      lObjRegExp.Global = True
      lObjRegExp.IgnoreCase = True
      Set lObjRegMatches = lObjRegExp.Execute(pStrDate)
      ' List each individual match and compare to different date functoids
      For Each lObjMatch In lObjRegMatches
            Select Case lObjMatch.Value
                  Case "a" : pStrDate = Replace(pStrDate,"a",LCase(Right(lDtmNow,2)))
                  Case "A" : pStrDate = Replace(pStrDate,"A",UCase(Right(lDtmNow,2)))
                  Case "B" : pStrDate = Replace(pStrDate,"B",InternetTime())
                  Case "d" : pStrDate = Replace(pStrDate,"d",LeadingZero(lLngDay))
                  Case "D" : pStrDate = Replace(pStrDate,"D",Left(WeekDayName(lLngWeekDay),3))
                  Case "F" : pStrDate = Replace(pStrDate,"F",MonthName(lLngMonth))
                  Case "g" : pStrDate = Replace(pStrDate,"g",FormatHour())
                  Case "G" : pStrDate = Replace(pStrDate,"G",lLngHour)
                  Case "h" : pStrDate = Replace(pStrDate,"h",LeadingZero(FormatHour()))
                  Case "H" : pStrDate = Replace(pStrDate,"H",LeadingZero(lLngHour))
                  Case "i" : pStrDate = Replace(pStrDate,"i",LeadingZero(lLngMinute))
                  Case "j" : pStrDate = Replace(pStrDate,"j",lLngDay)
                  Case "l" : pStrDate = Replace(pStrDate,"l",WeekDayName(lLngWeekDay))
                  Case "L" : pStrDate = Replace(pStrDate,"L",IsLeap())
                  Case "m" : pStrDate = Replace(pStrDate,"m",LeadingZero(lLngMonth))
                  Case "M" : pStrDate = Replace(pStrDate,"M",Left(MonthName(lLngMonth),3))
                  Case "n" : pStrDate = Replace(pStrDate,"n",lLngMonth)
                  Case "r" : pStrDate = Replace(pStrDate,"r",Left(WeekDayName(lLngWeekDay),3) & ", " & _
                        lLngDay & " " & Left(MonthName(lLngMonth),3) & " " & lLngYear & " " & FormatDateTime(DateToFormat,4))
                  Case "s" : pStrDate = Replace(pStrDate,"s",LeadingZero(lLngSecond))
                  Case "S" : pStrDate = Replace(pStrDate,"S",OrdinalSuffix())
                  Case "t" : pStrDate = Replace(pStrDate,"t",DaysInMonth())
                  Case "U" : pStrDate = Replace(pStrDate,"U",DateDiff("s",DateSerial(1970, 1, 1),lDtmNow))
                  Case "w" : pStrDate = Replace(pStrDate,"w",lLngWeekDay - 1)
                  Case "W" : pStrDate = Replace(pStrDate,"W",DatePart("ww", Date(), 2, vbFirstFourDays))
                  Case "Y" : pStrDate = Replace(pStrDate,"Y",lLngYear)
                  Case "y" : pStrDate = Replace(pStrDate,"y",Right(lLngYear,2))
                  Case "z" : pStrDate = Replace(pStrDate,"z",DatePart("d", Date(), 2))
                  Case Else : pStrDate = pStrDate & ""
            End Select
      Next
      Set lObjRegExp = Nothing
      FormatDate = pStrDate
End Function
0
 
strickddCommented:
This is a very short and to-the-point way of doing it.

http://devguru.com/technologies/vbscript/13887.asp
0
 
jitgangulyCommented:
How does this work ?

aspStartDate = Year(date()) & "-" & month(date()) & "-" & day(date())

response.write("Start Date" & aspStartDate & "<br>")
0
 
Gemini532Author Commented:
Thank you guys!  I got it to work   :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now