JS Date Processing

Source: Internet
Author: User

Format of JS string to date:

// Format the date
Function dateformat (d ){
VaR da = new date (D. Replace (/-/g ,"/"));// This sentence is found on the internet, and many people make mistakes. The following/is followed by a space.
Return da. Format ("yyyy-mm-dd"); // execute the following date formatting Method
}

JS date formatting:

Date. Prototype. format = function (Format) // Author: meizz
{
VaR o = {
"M +": This. getmonth () + 1, // month
"D +": This. getdate (), // day
"H +": This. gethours (), // hour
"M +": This. getminutes (), // minute
"S +": This. getseconds (), // second
"Q +": Math. Floor (this. getmonth () + 3)/3), // quarter
"S": This. getmilliseconds () // millisecond
}
If (/(Y +)/. Test (Format) format = format. Replace (Regexp. $1,
(This. getfullyear () + ""). substr (4-Regexp. $1. Length ));
For (var k in O) if (New Regexp ("(" + K + ")"). Test (Format ))
Format = format. Replace (Regexp. $1,
Regexp. $1. Length = 1? O [k]:
("00" + O [k]). substr ("" + O [k]). Length ));
Return format;
}

Usage:

VaR d = "12:12:12 ";

VaR da = new date (D. Replace (/-/g ,"/"));

Alert (new date (). Format ("yyyy-mm-dd "));
Alert (new date ("January 12 2008"
11:12:30 "). Format (" yyyy-mm-dd hh: mm: SS "));

Convert JS string to date type

Convert a date string such as "10:18:30" to a date object:

VaRS = "09:41:30";VaRD =NewDate (date. parse (S. Replace (/-/g ,"/")));

 

 

 

 

 

 

 

 

 

 

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.