JavaScript implementation of date formatting, supporting n multiple formatted date types

Source: Internet
Author: User

/** * Date format * @param format * Format parameter, support various date formats * @returns {string} * return formatted date*//*eslint-disable no-extend-native*//*eslint-disable Indent*/Date.prototype.dateFormart= Date.prototype.dateFormart | |function(format) {Console.log ( This)    /*eslint-disable No-useless-escape*/Let formatString= Format.match (/[a-za-z]{1,4}|[ \--\/-year-month-day-time-minute-second-\s-:]/g) Let date= []     for(Let i = 0, len = formatstring.length; i < Len; i++) {      Switch(Formatstring[i]) { Case' YYYY ': Date.push ( This. getFullYear ()) Break         Case' YY ': Date.push ( This. GetYear ()) Break         Case' MM ': Let month= This. GetMonth () + 1Date.push (Dnumber (month)) Break         CaseM: Date.push ( This. GetMonth () + 1)           Break         Case' DD ': Date.push (Dnumber ( This. GetDate ()))  Break         Case' d ': Date.push ( This. GetDate ()) Break         Case' HH ': Date.push (Dnumber ( This. GetHours ()))  Break         CaseH: Date.push ( This. GetHours ()) Break         Case' mm ': Date.push (Dnumber ( This. getminutes ()))  Break         Case' m ': Date.push ( This. getminutes ()) Break         Case' SS ': Date.push (Dnumber ( This. getseconds ()))  Break         Case' s ': Date.push ( This. getseconds ()) Break        default: Date.push (Formatstring[i]) Break      }    }    returnDate.join (' ')  }

JavaScript implementation of date formatting, supporting n multiple formatted date types

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.