Javascript date formatting example

Source: Internet
Author: User

Javascript date formatting example

This article mainly introduces the javascript date formatting example. For more information, see

The Code is as follows:

/**

* Format the date

* @ Param {Object} The MS specifies the number of milliseconds for the global standard time between the date and midnight, January 1, January 1, 1970

* @ Return year-month-date hh: mm

*/

Util. parseToDate = functions (MS ){

Var date = (new Date (parseInt (MS )));

Return formatDate (date, "yyyy-MM-dd mm: hh ");

};

/**

* Format the date

* <Code>

* Yyyy ------- year

* MM --------- month

* Dd --------- day

* Hh --------- hour

* Mm --------- minute

* For example, Util. formatDate (new Date (), 'yyyy-MM-dd mm: hh ');

* Or Util. formateDate (new Date (), 'yyyy/MM/dd mm/hh ');

* </Code> * @ param {Date} date the Date object to be formatted

* @ Param {Object} style

* @ Return returns the formatted current time.

*/

Util. formatDate = function (date, style ){

Var y = date. getFullYear ();

Var M = "0" + (date. getMonth () + 1 );

M = M. substring (m. length-2 );

Var d = "0" + date. getDate ();

D = d. substring (d. length-2 );

Var h = "0" + date. getHours ();

H = h. substring (h. length-2 );

Var m = "0" + date. getMinutes ();

M = m. substring (m. length-2 );

Return style. replace ('yyyy', y ). replace ('mm', M ). replace ('dd', d ). replace ('hh ', h ). replace ('mm', m );

}

 

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.