custom software design

ArticlesFormatting Dates/Times in ASP, PHP, Cold Fusion, VB.NET Notes
custom software design

Ever find yourself needing to remember how to format a date or time in a particular programming language? Here is a list of different programming languages and how to format a date or time in that language.

PHP
$current_date=date("m/d/Y"); // returns 09/15/2007
$current_time_military=date("H:i:s"); // returns 18:09:54 time in military
$current_time_ampm=date("h:i:s a"); // returns 06:09:54 pm time in 12 hour format am/pm
$date_time_for_mysql=date("Y-m-d")." ".date("H:i:s"); // returns "2007-09-15 18:09:54".

$olddate="03/21/2004";
$mynewdate=date("Y-m-d", strtotime($olddate)); // Converts a Date Formatted "03/21/2004"
echo($mynewdate); // To New Date Format "2004-03-21" (Used by MySql Date Field)

Use this to format date/time when constructing MySQL Insert/Update Statements

Click here for full list of format options...

Classic ASP
x=now()
current_date_time=month(x) & "/" & day(x) & "/" & year(x) 'returns 9/17/2007
current_time=hour(x) & ":" & minute(x) & ":" & second(x) 'returns 18:14:25 military time

VB.NET/ASP.NET
Dim x
x=Now
current_date=Format(Now, "MM/dd/yyyy") 'Returns 09/15/2007
current_time=Format(Now, "hh:mm:ss tt") 'Returns 10:23:45 PM
current_time_military=Format(Now, "HH:mm:ss")'Returns 22:23:45 in military
current_time_military=FormatDateTime(Now, vbShortTime) 'Returns 22:23 in military

Cold Fusion

#DateFormat(todayDate, "mm/dd/yyyy")# Rem returns 09/16/2008
#TimeFormat(todayDate, "HH:mm:ss")# Rem returns 21:12:07 military
#TimeFormat(todayDate, "hh:mm:sstt")# Rem returns 09:12:07 pm