• Home
  • Services
  • Profile
  • Portfolio
  • Contact
  • Links
  • Blog
  • Archives
  • Sitemap
Subscribe: Posts | Comments | E-mail
  • Ajax XMLHttpRequestAsynchronous HTTP Request How To's
  • FreebiesBest Freebies Collection
  • MiscellaneousNews, Announcements & General Stuffs
  • Print & Web DesignPhotoshop, Vector, CSS, XHTML
  • ProgrammingProgramming PHP, ASP, .NET and more

Bali Web Design

Posted on April 22, 2008 - by webmaster

VBscript/ASP Classic Date Format Unix Timestamp

Programming

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:
    1. %A – AM or PM
    2. %a – am or pm
    3. %m – Month with leading zeroes (01 – 12)
    4. %n – Month without leading zeroes (1 – 12)
    5. %F – Month name (January – December)
    6. %M – Three letter month name (Jan – Dec)
    7. %d – Day with leading zeroes (01 – 31)
    8. %j – Day without leading zeroes (1 – 31)
    9. %H – Hour with leading zeroes (12 hour)
    10. %h – Hour with leading zeroes (24 hour)
    11. %G – Hour without leading zeroes (12 hour)
    12. %g – Hour without leading zeroes (24 hour)
    13. %i – Minute with leading zeroes (01 to 60)
    14. %I – Minute without leading zeroes (1 to 60)
    15. %s – Second with leading zeroes (01 to 60)
    16. %S – Second without leading zeroes (1 to 60)
    17. %L – Number of day of week (1 to 7)
    18. %l – Name of day of week (Sunday to Saturday)
    19. %D – Three letter name of day of week (Sun to Sat)
    20. %O – Ordinal suffix (st, nd rd, th)
    21. %U – UNIX Timestamp
    22. %Y – Four digit year (2003)
    23. %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>"&amp;vbcrlf)
Response.Write("Timestamp Format => "&amp;current_timestamp&amp;vbcrlf)
Response.Write("Regular Format => "&amp;from_unix_timestamp(current_timestamp)&amp;vbcrlf)
Response.Write("Advanced Format => "&amp;FormatDate("%D %M %d %Y %H:%i:%s %A",current_timestamp)&amp;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>"&amp;vbcrlf)
Response.Write("Timestamp Format => "&amp;nextweek&amp;vbcrlf)
Response.Write("Regular Format => "&amp;from_unix_timestamp(nextweek)&amp;vbcrlf)
Response.Write("Advanced Format => "&amp;FormatDate("%D %M %d %Y %H:%i:%s %a",nextweek)&amp;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
Share this article:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • MySpace
  • Ping.fm
  • Pownce
  • Slashdot
  • StumbleUpon
  • Technorati
  • Tumblr
  • TwitThis
This entry was posted on Tuesday, April 22nd, 2008 at 17:52 and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

5 Comments

We'd love to hear yours!



  1. Visit My Website

    August 16, 2008

    Permalink

    Jane said:

    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.



  2. Visit My Website

    November 24, 2008

    Permalink

    Vincent said:

    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)



  3. Visit My Website

    September 18, 2009

    Permalink

    ASP Classic relative date function | ASP Blog said:

    [...] 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 [...]



  4. Visit My Website

    September 18, 2009

    Permalink

    ASP Twitter Lib Update said:

    [...] 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 [...]



  5. Visit My Website

    September 29, 2009

    Permalink

    Classic ASP Relative Date « Bali Web Design Studio said:

    [...] 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 [...]



Leave a Comment

Here's your chance to speak.

  1. Name (required)

    Mail (required)

    Website

    Message

  • Most Popular Posts

    • { 108 } ResponsesPHP ajax login form using Jquery
    • { 79 } Responsesphp ajax tutorial create ajax based login form using jquery
    • { 71 } ResponsesASP Classic Programming Still Alive
    • { 53 } Responses63+ best practice to optimize PHP code performances
    • { 44 } ResponsesVideobox: Lightbox for videos
  • Latest Posts

    • PHP Newbie: Proper handling of Error & Exception in PHP
    • Why SEO lost importance
    • jQuery Mobile Announced : Touch-Optimized Web Framework for Smartphones & Tablets
    • Multiple Google Account Signin
    • Install, Configure & Running Memcache in Windows as Service
    • DynamicWP Image Cube Wordpress Plugin
    • New Wordpress 3.0 API menu_page_url
    • Wordpress 3.0 Custom Taxonomy
    • JQuery DataGrid Plugin Compass
    • Upgrading to upcoming CodeIgniter 2.0
  • Categories

    • advertorial
    • Ajax XMLHttpRequest
    • Featured
    • Freebies
    • Miscellaneous
    • Print & Web Design
    • Programming
    • Tutorials
    • Web 2.0
  • Featured News

    • PHP Newbie: Proper handling of Error & Exception in PHP by webmaster on August 18, 2010
    • Why SEO lost importance by webmaster on August 16, 2010
    • jQuery Mobile Announced : Touch-Optimized Web Framework for Smartphones & Tablets by webmaster on August 16, 2010
    • Install, Configure & Running Memcache in Windows as Service by webmaster on June 27, 2010
    • Website Optimization Best Practices, speed up your website load by ruby on May 21, 2009
  • Tag Cloud

    • Actionscript advertorial Ajax XMLHttpRequest API best practices CakePHP chat Classic ASP CMS CSS Development facebook firefox Flash flickr Freebies gmail HTML icons Javascript Jobs jQuery jQuery Plugins Library Lightbox memcache Mootools news Personal PHP PHP Frameworks plurk Print & Web Design prism Prototype regex regular expression Scriptaculous SEO Tutorial twitter Web 2.0 Wordpress Works YUI
  • Archives

    • August 2010
    • June 2010
    • May 2010
    • September 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
copyleft 2007 - 2010 Bali Website Design Studio