JavaScript format Date-time methods and custom formatting functions examples

Source: Internet
Author: User
Tags local time

 javascript Default time format We don't normally use it, so we need to format it, and here's what I've summed up about the JavaScript time formatting method

Many times, we can use the built-in methods of the Date object in JavaScript to format it, such as:   code as follows: var d = new Date (); Console.log (d); Output: Mon Nov 2013 21:50:33 gmt+0800 (China Standard Time) Console.log (d.todatestring ()); Date string, Output: Mon Nov 2013 Console.log (d.togmtstring ()); GMT, Output: Mon, Nov 2013 14:03:05 GMT Console.log (d.toisostring ()); International Standard Organization (ISO) format, output: 2013-11-04t14:03:05.420z console.log (D.tojson ()); Output: 2013-11-04t14:03:05.420z console.log (D.tolocaledatestring ()); Convert to local date format, depending on the environment, output: November 4, 2013 Console.log (D.tolocalestring ()); Convert to local date and time format, depending on the environment, output: November 4, 2013 afternoon 10:03:05 Console.log (d.tolocaletimestring ()); Convert to local time format, depending on the environment, output: PM 10:03:05 console.log (d.tostring ()); Convert to String, output: Mon Nov 2013 22:03:05 gmt+0800 (China Standard Time) Console.log (d.totimestring ()); Convert to time string, output: 22:03:05 gmt+0800 (China Standard Time) Console.log (d.toutcstring ()); Convert to World time, output: Mon, Nov 2013 14:03:05 GMT   If the above method does not meet our requirements, we can also customize the function to format the time, such as:   code as follows: Date.prototype.format = function (format) {       var date = {  &nbsP           "m+": This.getmonth () + 1,               "d+": this. GetDate (),               "h+": this.gethours (),           &NBS P   "m+": this.getminutes ()               "s+": This.getseconds (),     &N Bsp         "q+": Math.floor (This.getmonth () + 3)/3),               "S+": This.getmilliseconds ()        };        if (/(y+)/i.test (format)) {              format = Format.re Place (Regexp.$1, (this.getfullyear () + "). substr (4-regexp.$1.length));                for (var k in date) {            &N Bsp if (new RegExp ("+ K +")). Test (format) {                    &NBSp;format = Format.replace (regexp.$1, regexp.$1.length = 1                            ? Date[k]: ("+ date[k]"). substr (("" "+ date[k]). length);               &nbsp        return format; var d = new Date (). Format (' Yyyy-mm-dd '); Console.log (d); 2013-11-04
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.