skip to main |
skip to sidebar
RSS Feeds
ASP.NET, JavaScript, Oracle and SQL articles and code.
ASP.NET, JavaScript, Oracle and SQL articles and code.
11:39 AM
Posted by Michael Heliso
Note: Please check JavaScript Utility Functions Note.
The following javascript function allows you to convert an UTC date into a user specified format. At this time the implementation will convert the UTC date into dd/mm/yyyy and dd/mm/yyyy hh:mm:ss formats. You can easily extend it's functionality.
/*Define the namespaces*/
var JavaScript = {};
JavaScript.Utils = {};
JavaScript.Utils.Date = {};
JavaScript.Utils.Date.UtcFormat = function(utcDate, format) {
if (!(typeof utcDate === "string"))
throw "UTC date must be a string.";
if (!format)
throw "invalid date format specified.";
//Extract UTC date components using regex.
var date = utcDate.match(/^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2}(?:\.\d+)?)(Z(([+-])(\d{2}):(\d{2}))?)$/i);
if (!date)
throw "Invalid UTC date format.";
var newDate = null;
switch (format) {
case "dd/mm/yyyy":
{
newDate = date[3] + "/" + date[2] + "/" + date[1];
}
break;
case "dd/mm/yyyy hh:mm:ss":
{
newDate = date[3] + "/" + date[2] + "/" + date[1] + " " + date[4] + ":" + date[5] + ":" + date[6];
}
break;
}
return newDate;
};
JavaScript.Utils.Date.ToUtcFormat = function(date) {
if (!(typeof date === "string"))
throw "Date must be a string.";
var ndate = date.match(/^(\d{2})\/(\d{2})\/(\d{4})$/i);
if (!ndate)
throw "Invalid date format. Format must be dd/mm/yyyy";
var newDate = ndate[3] + "-" + ndate[2] + "-" + ndate[1] + "T00:00:00Z";
return newDate;
December 27, 2009 at 4:21:00 AM PST
What kind of links would you like?