JavaScript function to formal datetime. To Try it in JS Fiddler, please click here
To test above JS function, following HTML can be used.
<body>
<button onclick="dateformat()">Click me</button>
<script>
function dateformat() {
var date = new Date();
var today = date.DateFormat("dd/MM/yyyy HH:mm:ss:Z");
//alert(date);
alert(today);
};
</script>
</body>
PS Hayer
// ReSharper disable once NativeTypePrototypeExtending Date.prototype.DateFormat = function (format) { var mm = this.getMonth() + 1; var dd = this.getDate(); var datePart, timePart; if (format === "yyyy-MM-dd") { return [ this.getFullYear(), (mm > 9 ? '' : '0') + mm, (dd > 9 ? '' : '0') + dd ].join('-'); } else if (format === "dd-MM-yyyy") { return [ (dd > 9 ? '' : '0') + dd, (mm > 9 ? '' : '0') + mm, this.getFullYear() ].join('-'); } else if (format === "yyyyMMdd") { return [ this.getFullYear(), (mm > 9 ? '' : '0') + mm, (dd > 9 ? '' : '0') + dd ].join(''); } else if (format === "dd-MM-yyyy") { return [ (dd > 9 ? '' : '0') + dd, (mm > 9 ? '' : '0') + mm, this.getFullYear() ].join('-'); } else if (format === "dd/MM/yyyy") { return [ (dd > 9 ? '' : '0') + dd, (mm > 9 ? '' : '0') + mm, this.getFullYear() ].join('/'); } else if (format === "dd/MM/yyyy HH:mm:ss") { datePart = [(dd > 9 ? '' : '0') + dd, (mm > 9 ? '' : '0') + mm, this.getFullYear()].join('/'); timePart = [" " + this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds()].join(":"); return [datePart, timePart].join(''); } else if (format === "dd/MM/yyyy HH:mm:ss:Z") { datePart = [(dd > 9 ? '' : '0') + dd, (mm > 9 ? '' : '0') + mm, this.getFullYear()].join('/'); timePart = [ " " + this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds() + ":" + this.getMilliseconds() ].join(":"); return [datePart, timePart].join(''); } else { return [ this.getFullYear(), (mm > 9 ? '' : '0') + mm, (dd > 9 ? '' : '0') + dd ].join('-'); } };
To test above JS function, following HTML can be used.
<body>
<button onclick="dateformat()">Click me</button>
<script>
function dateformat() {
var date = new Date();
var today = date.DateFormat("dd/MM/yyyy HH:mm:ss:Z");
//alert(date);
alert(today);
};
</script>
</body>
PS Hayer
Please check my other (Dynamics CRM) blog here:
Hayer's MS Dynamics CRM Blog