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;
};


Next JavaScript function will convert date of dd/mm/yyyy format in UTC format. By default the time section will be ignored, being initialized with 00:00:00.


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;