Formatting the Date in Javascript


Date.prototype.toFormattedString = function (format) {
var date = this;
var formatted = “”;
formatted = formatted + format.replace(/dd|mm|yyyy|MM|hh|ss|ms|AMPM|\s|\/|\-|,|\./ig,
function match() {
switch (arguments[0]) {
case “dd”:
var dd = date.getDate();
return (dd < 10) ? "0" + dd : dd;
case "mm":
var mm = date.getMonth() + 1;
return (mm < 10) ? "0" + mm : mm;
case "yyyy": return date.getFullYear();
case "hh":
var hh = date.getHours();
return (hh < 10) ? "0" + hh : hh;
case "MM":
var MM = date.getMinutes();
return (MM < 10) ? "0" + MM : MM;
case "ss":
var ss = date.getSeconds();
return (ss < 10) ? "0" + ss : ss;
case "ms": return date.getMilliseconds();
case "AMPM":
var ampm = date.getHours();
return (ampm < 12) ? "AM" : "PM";
default: return arguments[0];
}
});

return formatted;
}

Example:
startDate.toFormattedString(“yyyy-mm-dd hh:MM:ss”);

About Kishore Dhanekula

MS Dynamics CRM and .NET Architect
This entry was posted in Ms Dynamics CRM 2013. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s