Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Format dates in ASP (VBScript)

Posted on 2006-05-22
5
Medium Priority
?
612 Views
Last Modified: 2008-03-06
     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
Comment
Question by:Gemini532
5 Comments
 
LVL 12

Assisted Solution

by:_Maddog_
_Maddog_ earned 400 total points
ID: 16734533
Hi Gemini532,

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

- Maddog
0
 
LVL 25

Assisted Solution

by:kevp75
kevp75 earned 400 total points
ID: 16734871
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
 
LVL 28

Assisted Solution

by:strickdd
strickdd earned 400 total points
ID: 16735394
This is a very short and to-the-point way of doing it.

http://devguru.com/technologies/vbscript/13887.asp
0
 
LVL 20

Accepted Solution

by:
jitganguly earned 800 total points
ID: 16736273
How does this work ?

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

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

Author Comment

by:Gemini532
ID: 16736960
Thank you guys!  I got it to work   :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question