Processing date in JS

Source: Internet
Author: User
Tags date1

These days have been entangled in various conversions of dates. Now, we have finally solved this problem. By the way, we have contributed to our achievements over the past few days and have grown together with everyone.

1. convert a date string to a date object.
A string in the format of yyyy-mm-dd is converted to a date object. You can use new date (date. parse (Str. Replace (/-/g ,"/")));
As follows:
VaR enddatestr = endyear + "-" + endmonth + "-" + endday;
Enddate = new date (date. parse (enddatestr. Replace (/-/g ,"/")));
Instance display:
Enddatestr: "2010-5-20"
Date. parse (enddatestr. Replace (/-/g, "/"): 1274284800000
Enddate: Thu May 20 00:00:00 UTC + 0800 2010

The format yyyy/mm/DD is converted into a date object. You can use new date (STR );
As follows:
VaR enddatestr = endyear + "/" + endmonth + "/" + endday;
Enddate = new date (enddatestr );
Instance display: "2010/5/20", Thu May 20 00:00:00 UTC + 0800 2010

The format yyyymmdd is converted to a date object. You can use new date (sdate. replace (/^ (\ D {4}) (\ D {2}) (\ D {2}) $ /, "$1/$2/$3 "));
As follows:
VaR sdate = "20090516 ";
Sdate = sdate. replace (/^ (\ D {4}) (\ D {2}) (\ D {2}) $/, "$1/$2/$3 ");
New Date (sdate );
Instance display: "2009/05/16", sat May 16 00:00:00 UTC + 0800 2009

2. display the next day of the current date.
VaR date = new date ();
VaR B = date. getdate ();
B + = 1;
Date. setdate (B );

3. Calculate the number of days between two dates.
VaR date1 = new date (0800, 28); // Mon Jul 28 00:00:00 UTC + 2008
VaR date2 = new date (2009, 5, 16); // Tue Jun 16 00:00:00 UTC + 0800 2009
VaR n = (date2.gettime ()-date1.gettime ()/(24*60*60*1000); // 323
VaR n = (Date2-Date1)/(24*60*60*1000); // directly subtract the number of milliseconds.

4. dateobj. getday () returns the date value of the Week represented by the local time in the date object.
The value returned by the getday method is an integer between 0 and 6. It represents a day in a week. The relationship between the return value and the date in a week is as follows:

Week
0 Sunday
1 Monday
2 Tuesday
3. Wednesday
4 Thursday
Friday
6. Saturday

This method can be used to determine 'workday.

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.