Posted on April 22, 2008 - by webmaster
VBscript/ASP Classic Date Format Unix Timestamp
Unix timestamps is a series of number which is represent the seconds since the epoch, January 1st 1970 00:00:00. With Unix timestamp format we can just simply store integer value into database careless about the specific date format, and next we can load it in necessary format in our webpage. This is which i think much simpler and safe rather than store it in regular date format.
PHP has load of date format options, we just need to specify the format we need, but in ASP especially ASP 3.0 (Classic ASP) which has only few format date available, we need to format it manually the beauty of classic asp programming, you need to create your own with limited built-in functionalities. I came a cross Dave Child’s article at ilovejackdaniels.com and follow the comments from respectives users thanks guys, here it is with little fixxing:
'###########################
'# @ to unix timestamp
'# @ param s => original date format
'###########################
function to_unix_timestamp(s)
to_unix_timestamp = DateDiff("s", "01/01/1970 00:00:00", s)
end function
Convert original date format into unix timestamp format. the parameter (s) is a valid date in regular format
'###########################
'# @ from unix timestamp
'# @ param s => int unix timestamp
'###########################
function from_unix_timestamp(s)
from_unix_timestamp = DateAdd("s", s, "01/01/1970 00:00:00")
end function
Convert unix timestamp format into regular date format. the parameter (s) is an integer unix timestamp
'###########################
'# DESC : advanced format date
'# %A - AM or PM
'# %a - am or pm
'# %m - Month with leading zeroes (01 - 12)
'# %n - Month without leading zeroes (1 - 12)
'# %F - Month name (January - December)
'# %M - Three letter month name (Jan - Dec)
'# %d - Day with leading zeroes (01 - 31)
'# %j - Day without leading zeroes (1 - 31)
'# %H - Hour with leading zeroes (12 hour)
'# %h - Hour with leading zeroes (24 hour)
'# %G - Hour without leading zeroes (12 hour)
'# %g - Hour without leading zeroes (24 hour)
'# %i - Minute with leading zeroes (01 to 60)
'# %I - Minute without leading zeroes (1 to 60)
'# %s - Second with leading zeroes (01 to 60)
'# %S - Second without leading zeroes (1 to 60)
'# %L - Number of day of week (1 to 7)
'# %l - Name of day of week (Sunday to Saturday)
'# %D - Three letter name of day of week (Sun to Sat)
'# %O - Ordinal suffix (st, nd rd, th)
'# %U - UNIX Timestamp
'# %Y - Four digit year (2003)
'# %y - Two digit year (03)
'###########################
function FormatDate(format, intTimeStamp)
'############################
'# prevent month name error
'############################
Dim monthname():Redim monthname(12)
monthname(1) = "January"
monthname(2) = "February"
monthname(3) = "March"
monthname(4) = "April"
monthname(5) = "May"
monthname(6) = "June"
monthname(7) = "July"
monthname(8) = "August"
monthname(9) = "September"
monthname(10) = "October"
monthname(11) = "November"
monthname(12) = "December"
'############################
dim OrigDateFormat, A
'############################
'IntTimeStamp
'############################
if not (isnumeric(intTimeStamp)) then
if isdate(intTimeStamp) then
intTimeStamp = DateDiff("S", "01/01/1970 00:00:00", intTimeStamp)
else
Response.Write("Invalid Date")
exit function
end if
end if
'############################
'original format
'############################
if (intTimeStamp=0) then
OrigDateFormat = now()
else
OrigDateFormat = DateAdd("s", intTimeStamp, "01/01/1970 00:00:00")
end if
OrigDateFormat = trim(OrigDateFormat)
'############################
'get current partial format
'############################
dim dateDay:dateDay=DatePart("d",OrigDateFormat)
dim dateMonth:dateMonth=DatePart("m",OrigDateFormat)
dim dateYear:dateYear=DatePart("yyyy",OrigDateFormat)
dim dateHour:dateHour=DatePart("h",OrigDateFormat)
dim dateMinute:dateMinute=DatePart("m",OrigDateFormat)
dim dateSecond:dateSecond=DatePart("s",OrigDateFormat)
'############################
'relpace
'############################
format = replace(format, "%Y", right(dateYear, 4))
format = replace(format, "%y", right(dateYear, 2))
format = replace(format, "%m", right("00"&dateMonth,2))
format = replace(format, "%n", cint(dateMonth))
format = replace(format, "%F", monthname(cint(dateMonth)))
format = replace(format, "%M", left(monthname(cint(dateMonth)), 3))
format = replace(format, "%d", right("00"&dateDay,2))
format = replace(format, "%j", cint(dateDay))
format = replace(format, "%h", right("00"&dateHour,2))
format = replace(format, "%g", cint(dateHour))
'############################
'12 hours
'############################
if (cint(dateHour) > 12) then
A = "PM"
else
A = "AM"
end if
format = replace(format, "%A", A)
format = replace(format, "%a", lcase(A))
if (A = "PM") then
format = replace(format, "%H", right("00" & (dateHour-12), 2))
else
format = replace(format, "%H", right("00"&(dateHour),2))
end if
if (A = "PM") then
format = replace(format, "%G", cint(dateHour)-12)
else
format = replace(format, "%G", cint(dateHour))
end if
'############################
'time
'############################
format = replace(format, "%i", right("00"&dateMinute,2))
format = replace(format, "%I", cint(dateMinute))
format = replace(format, "%s", right("00"&dateSecond,2))
format = replace(format, "%S", cint(dateSecond))
format = replace(format, "%L", WeekDay(OrigDateFormat))
format = replace(format, "%D", left(WeekDayName(WeekDay(OrigDateFormat)), 3))
format = replace(format, "%l", WeekDayName(WeekDay(OrigDateFormat)))
format = replace(format, "%U", intTimeStamp)
format = replace(format, "11%O", "11th")
format = replace(format, "1%O", "1st")
format = replace(format, "12%O", "12th")
format = replace(format, "2%O", "2nd")
format = replace(format, "13%O", "13th")
format = replace(format, "3%O", "3rd")
format = replace(format, "%O", "th")
'############################
'return
'############################
FormatDate = format
end function
Advanced date format as PHP date() function, function parameters are:
- format is string or series of strings with recognized format charaters pattern:
- %A – AM or PM
- %a – am or pm
- %m – Month with leading zeroes (01 – 12)
- %n – Month without leading zeroes (1 – 12)
- %F – Month name (January – December)
- %M – Three letter month name (Jan – Dec)
- %d – Day with leading zeroes (01 – 31)
- %j – Day without leading zeroes (1 – 31)
- %H – Hour with leading zeroes (12 hour)
- %h – Hour with leading zeroes (24 hour)
- %G – Hour without leading zeroes (12 hour)
- %g – Hour without leading zeroes (24 hour)
- %i – Minute with leading zeroes (01 to 60)
- %I – Minute without leading zeroes (1 to 60)
- %s – Second with leading zeroes (01 to 60)
- %S – Second without leading zeroes (1 to 60)
- %L – Number of day of week (1 to 7)
- %l – Name of day of week (Sunday to Saturday)
- %D – Three letter name of day of week (Sun to Sat)
- %O – Ordinal suffix (st, nd rd, th)
- %U – UNIX Timestamp
- %Y – Four digit year (2003)
- %y – Two digit year (03)
- intTimeStamp series of integer represent valid unix timestamp
Sample of usage:
'################################
'# CURRENT DATE
'################################
current_date_time = now()
current_timestamp = to_unix_timestamp(current_date_time)
Response.Write("<h3>TODAY:</h3>"&vbcrlf)
Response.Write("Timestamp Format => "&current_timestamp&vbcrlf)
Response.Write("Regular Format => "&from_unix_timestamp(current_timestamp)&vbcrlf)
Response.Write("Advanced Format => "&FormatDate("%D %M %d %Y %H:%i:%s %A",current_timestamp)&vbcrlf)
Sample above is about writing current date time in different format, The result of sample above is:
<h3>TODAY:</h3> Timestamp Format => 1208920980 Regular Format => 4/23/2008 3:23:00 AM Advanced Format => Wed Apr 23 2008 03:04:00 AM
'################################
'# Next Week
'################################
current_date_time = now()
nextweek = to_unix_timestamp(current_date_time)+(7 * 24 * 60 * 60)
Response.Write("<h3>NEXT WEEK:</h3>"&vbcrlf)
Response.Write("Timestamp Format => "&nextweek&vbcrlf)
Response.Write("Regular Format => "&from_unix_timestamp(nextweek)&vbcrlf)
Response.Write("Advanced Format => "&FormatDate("%D %M %d %Y %H:%i:%s %a",nextweek)&vbcrlf)
Sample above is about writing the next week date time in different format, (7 * 24 * 60 * 60) means 7 days, 24 hours, 60 minutes, 60 seconds. The result is:
<h3>NEXT WEEK:</h3> Timestamp Format => 1209525780 Regular Format => 4/30/2008 3:23:00 AM Advanced Format => Wed Apr 30 2008 03:04:00 am

Visit My Website
August 16, 2008
Permalink
Hi,
I am using
http://www.mobilefish.com/services/unixtimestamp/unixtimestamp.php to convert Unix timestamp into a meaningful date and time.
This site also contains other useful tools.
Visit My Website
November 24, 2008
Permalink
Thanks for the FormatDate function, only a small bug in it:
dim dateMinute:dateMinute=DatePart(“m”,OrigDateFormat)
should be
dim dateMinute:dateMinute=DatePart(“n”,OrigDateFormat)
Visit My Website
September 18, 2009
Permalink
[...] date function into my asp twitter library, and comes across my old post at other blog about converting ASP date into Timestamp format and vice versa, from timestamp format we simply need to calculate the time different between [...]
Visit My Website
September 18, 2009
Permalink
[...] Lib, a couple functions changes, and adding relative date function this could be done by importing ASP Timestamp Date function at my old post which is based on a great ASP Timestamp Function by Dave [...]
Visit My Website
September 29, 2009
Permalink
[...] use unix timestamp format do calculate the time margin via classic asp timestamp function, so it is easy to extend the functionality for different purposes for example display relative date [...]