Displaying the date and time in a specified format is very useful for developing time-related applications. Next, let's share with you how to format and display the date and time. For more information, see
<Html> <body> script /// <summary> /// format the display Date and Time /// </summary> /// <param name = "x"> date and time to be displayed, for example, new Date () </param> // <param name = "y"> the format to be displayed, for example, yyyy-MM-dd hh: mm: ss </param> function date2str (x, y) {var z = {M: x. getMonth () + 1, d: x. getDate (), h: x. getHours (), m: x. getMinutes (), s: x. getSeconds ()}; y = y. replace (/(M + | d + | h + | m + | s +)/g, function (v) {return (v. length> 1? "0": "") + eval ('z. '+ v. slice (-1 ))). slice (-2)}); return y. replace (/(y +)/g, function (v) {return x. getFullYear (). toString (). slice (-v. length)});} alert (date2str (new Date (), "yyyy-MM-dd hh: mm: ss"); alert (date2str (new Date (), "yyyy-M-d h: m: s"); script </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
The current time.
The Code is as follows:
// 1.2013-4-9 11:21:32
// 2.2013-, January 1, April 9
// April 9 11:21:32 A.M.
// April 9 13:21:32 P.M.
/*
Var thedate = new Date ();
Alert (thedate. getFullYear () + '-' + thedate. getMonth () + '-' + thedate. getDate () + ''+ thedate. toLocaleTimeString ());
Alert (thedate. toLocaleDateString () + ''+ thedate. getHours () + 'point' + thedate. getMinutes () + 'mine' + thedate. getSeconds () + 'second ');
If (thedate. getHours () <12 ){
Alert (thedate. toLocaleDateString () + 'morning '+ thedate. getHours () + 'point' + thedate. getMinutes () + 'mine' + thedate. getSeconds () + 'second ');
}
Else {
Alert (thedate. toLocaleDateString () + 'Afternoon '+ thedate. getHours () + 'point' + thedate. getMinutes () + 'mine' + thedate. getSeconds () + 'second ');
}
*/