Javascript code _ javascript tips

Source: Internet
Author: User
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 ');
}
*/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.