Adding to what Lankymart linked.
ASP Classic has many time / date options and from my experience it seems you have to create a function or sub for every different type.
My HTTP Only cookie GMT is different from my GMT RSS Feed layout.
Example 1: strGMTDateRFC22 = CookieServerUTC("d",1,5,"GMT")
'# following formating RFC22 for your GMT Cookie time.
strGMTDateRFC22 = CookieServerUTC("d","&strCookieExpires&",5,"GMT") ' 1 Day set in char enc dec page
Response.AddHeader "Set-Cookie", strCookieName & "=" & strCookieKey & "=" & strCookieValue & "; expires=" & strGMTDateRFC22 & "; domain="& strCookieDomain &"; path=/; HTTPOnly"
First of two functions
Function CookieServerUTC(strD,strT,strOSet,strZ)
Dim strTG,strCookieD
'snipped unwanted code
strTG = DateAdd("h", strOSet,Now())
strCookieD = DateAdd(strD,strT,CDate(strTG))
CookieServerUTC = fncFmtDate(strCookieD, "%a, %d %b %Y %H:%N:%S "&strZ&"")
End Function
Another example when you need to setup Server UTC
This is allows for strH = "h" strT = "5" (strT Time Offset +/-) and strZ GMT (Timezone)
Function GetServerUTC(strH,strT,strZ)
GetServerUTC = fncFmtDate(DateAdd(strH,strT,Now()), "%a, %d %b %Y %H:%N:%S "&strZ&"")
End Function
And then the function script that was published back in 2001 when ASP Classic was still HOT. The 4guysfromrolla.com posted it and I can imagine it has helped many Time Date format enthusiasts.
Here's the link and blow that is the code just in case 2001 is deleted from the internet.
Customizable Date Formatting Routine by Ken Schaefer
Function fncFmtDate( _
byVal strDate, _
byRef strFormat _
)
' Accepts strDate as a valid date/time,
' strFormat as the output template.
' The function finds each item in the
' template and replaces it with the
' relevant information extracted from strDate
' Template items (example)
' %m Month as a decimal (02)
' %B Full month name (February)
' %b Abbreviated month name (Feb )
' %d Day of the month (23)
' %O Ordinal of day of month (eg st or rd or nd)
' %j Day of the year (54)
' %Y Year with century (1998)
' %y Year without century (98)
' %w Weekday as integer (0 is Sunday)
' %a Abbreviated day name (Fri)
' %A Weekday Name (Friday)
' %H Hour in 24 hour format (24)
' %h Hour in 12 hour format (12)
' %N Minute as an integer (01)
' %n Minute as optional if minute <> 0
' %S Second as an integer (55)
' %P AM/PM Indicator (PM)
'#### READ THE FORMATTING GUIDE ABOVE #####
'Snipped the code due to the 1% possibility you didn't format your date / time correctly and the code tosses an error based on that formatting.
End Function ' fncFmtDate
As you can see we have many possible solutions.
Find the date time formatting solution that fits your projects and built on top of it.
Ken's code did require a specific input date format.
For those that are not really into debugging or creating their own code you can use what I have built from Ken's example above.
This takes just any format in ASP Classic and works it's magic.
Notice the first part of the code corrects single digit issues.
If you have ever worked with currency formatting you should know about this.
Murray's Active DateFormat to Anything Code reworking what Ken started.
Here's the magic part.
if (DatePart("m", strDate) < 10) then
twoDigMonth = "0" & DatePart("m", strDate)
else
twoDigMonth = DatePart("m", strDate)
end if
if (DatePart("d", strDate) < 10) then
twoDigDay = "0" & DatePart("d", strDate)
else
twoDigDay = DatePart("d", strDate)
end if
Then here's the rest of the code.
Take out all the "On Error Resume Next" because they are not needed as long as your input is corrected.
' Insert Month Numbers
strFormat = Replace(strFormat, "%m", _
DatePart("m", strDate), 1, -1, vbBinaryCompare)
' Insert Month Numbers
strFormat = Replace(strFormat, "%M", _
twoDigMonth, 1, -1, vbBinaryCompare)
' Insert non-Abbreviated Month Names
strFormat = Replace(strFormat, "%B", _
MonthName(DatePart("m", strDate), _
False), 1, -1, vbBinaryCompare)
' Insert Abbreviated Month Names
strFormat = Replace(strFormat, "%b", _
MonthName(DatePart("m", strDate), _
True), 1, -1, vbBinaryCompare)
' Insert Day Of Month
strFormat = Replace(strFormat, "%d", _
DatePart("d",strDate), 1, _
-1, vbBinaryCompare)
' Insert Day Of Month
strFormat = Replace(strFormat, "%D", _
twoDigDay, 1, _
-1, vbBinaryCompare)
' Insert Day of Month Ordinal (eg st, th, or rd)
strFormat = Replace(strFormat, "%O", _
fncGetDayOrdinal(Day(strDate)), _
1, -1, vbBinaryCompare)
' Insert Day of Year
strFormat = Replace(strFormat, "%j", _
DatePart("y",strDate), 1, _
-1, vbBinaryCompare)
' Insert Long Year (4 digit)
strFormat = Replace(strFormat, "%Y", _
DatePart("yyyy",strDate), 1, _
-1, vbBinaryCompare)
' Insert Short Year (2 digit)
strFormat = Replace(strFormat, "%y", _
Right(DatePart("yyyy",strDate),2), _
1, -1, vbBinaryCompare)
' Insert Weekday as Integer (eg 0 = Sunday)
strFormat = Replace(strFormat, "%w", _
DatePart("w",strDate,1), 1, _
-1, vbBinaryCompare)
' Insert Abbreviated Weekday Name (eg Sun)
strFormat = Replace(strFormat, "%a", _
WeekDayName(DatePart("w",strDate,1),True), 1, _
-1, vbBinaryCompare)
' Insert non-Abbreviated Weekday Name
strFormat = Replace(strFormat, "%A", _
WeekDayName(DatePart("w",strDate,1),False), 1, _
-1, vbBinaryCompare)
' Insert Hour in 24hr format
str24HourPart = DatePart("h",strDate)
If Len(str24HourPart) < 2 then str24HourPart = "0" & _
str24HourPart
strFormat = Replace(strFormat, "%H", str24HourPart, 1, _
-1, vbBinaryCompare)
' Insert Hour in 12hr format
int12HourPart = DatePart("h",strDate) Mod 12
If int12HourPart = 0 then int12HourPart = 12
strFormat = Replace(strFormat, "%h", int12HourPart, 1, _
-1, vbBinaryCompare)
' Insert Minutes
strMinutePart = DatePart("n",strDate)
If Len(strMinutePart) < 2 then _
strMinutePart = "0" & strMinutePart
strFormat = Replace(strFormat, "%N", strMinutePart, _
1, -1, vbBinaryCompare)
' Insert Optional Minutes
If CInt(strMinutePart) = 0 then
strFormat = Replace(strFormat, "%n", "", 1, _
-1, vbBinaryCompare)
Else
If CInt(strMinutePart) < 10 then _
strMinutePart = "0" & strMinutePart
strMinutePart = ":" & strMinutePart
strFormat = Replace(strFormat, "%n", strMinutePart, _
1, -1, vbBinaryCompare)
End if
' Insert Seconds
strSecondPart = DatePart("s",strDate)
If Len(strSecondPart) < 2 then _
strSecondPart = "0" & strSecondPart
strFormat = Replace(strFormat, "%S", strSecondPart, 1, _
-1, vbBinaryCompare)
' Insert AM/PM indicator
If DatePart("h",strDate) >= 12 then
strAMPM = "PM"
Else
strAMPM = "AM"
End If
strFormat = Replace(strFormat, "%P", strAMPM, 1, _
-1, vbBinaryCompare)
fncFmtDate = strFormat
Bundle this all into a nice function call.
Function fncFmtDate(strDate, strFormat)
'Example for your perfect GMT time in Sitemaps, RSSFeeds, Google Stuff, Cookies
'# I'm in Louisiana so I'm -6 hours from GMT
'# fncFmtDate(DateAdd("h", -6,Now()), "%a, %d %b %H:%N:%S GMT")&"
'# Place code above here.... Don't forget the Magic part.
End Function
There you have it, 100% working live code.
If you see an error it's your input not the function. Be sure to follow the formatting example of the GMT I listed.
We can't give perfect solutions for every ASP Classic programmer, we give what has worked for us and basic code practices.
If I thought this was all about Copy and Paste solutions I would have been posting since the 80's.
Give it time, work with error traps and learn to read from left to right and top to bottom.
Solution to format placed here: Issue: It outputs : Server current
UTC time is: Fri, 15 Jan 2016 07:42:13 UTC But I need in this format:
YYYYMMDDHHMMSS
Here's how you use the function to get your format. See how I changed the %a to %Y ? The intructions are in the code.
fncFmtDate(DateAdd("h", -6,Now()), "%Y, %m %d %H:%N:%S GMT")
If you do not need spaces or the comma take it out. But how you show your desired format is something I have not seen in ASP development.
fncFmtDate(DateAdd("h", -6,Now()), ""%Y%m%d%H%N%S GMT")
Work with the code, tell it what you want as your output, read the code.