JS time processing and format conversion

Source: Internet
Author: User
I. Time addition and subtraction, date addition and subtraction, the same as year, month, and day addition and subtraction. VarmydatenewDate (& amp; #39; 2012-05-10:10:21 & amp; #39;); mydate. setDate (mydate. getDate () + 50); // The current time plus 50 days and two days. Plus or minus minutes, setMinutes method format conversion Date. p...

I. Time addition and subtraction, date addition and subtraction, the same as year, month, and day addition and subtraction.
Var mydate = new Date ('2017-05-10 10:10:21 ');
Mydate. setDate (mydate. getDate () + 50); // Add 50 days to the current time

II. Plus or minus minutes, setMinutes Method
 

Format Conversion
Date. prototype. format = function (format)
{
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;
}
 

 
Var mydate = new Date ('2017-05-10 10:10:21 ');
Mydate. setDate (mydate. getDate () + 50 );
Mydate. setMinutes (min, sec, MS)
Alert (mydate. format ("yyyy-MM-dd hh: mm: ss "));

 

 

From mixer-

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.