JavaScript Date collection (Today, yesterday, this Monday, weekend, early month, month end)

Source: Internet
Author: User

Small Talk: The first day of the new year to work, looking at the end of their 15 years writing code, really feel very bad, because there is no time to write at the end, a thought to do backstage management need to get a week start and end date, panic, the project to test it, there are many things not done, Directly copied a section of the online error code, in fact, really simple, today re-spend some time to encapsulate, for later use, big God do not spray, thank you!

First, package for the jquery version of the date collection plug-in
Jquery.datecollections = (function(){    varNowdate =NewDate (); varClonenowdate =NewDate (); varFullyear =nowdate.getfullyear (); varmonth = Nowdate.getmonth () + 1;//the GetMonth method returns 0-11, representing January-December    varDate =nowdate.getdate (); varEndOfMonth =NewDate (fullyear, month, 0). GetDate ();//get the last day of the month    //formatted Date (2016-02-14)    functiongetfulldate (targetdate) {vard, Y, M, D; if(targetdate) {D=NewDate (targetdate); Y=d.getfullyear (); M= D.getmonth () + 1; D=d.getdate (); } Else{y=fullyear; M=month; D=date; } m= m > 9? M: ' 0 ' +m; D= d > 9? D: ' 0 ' +D; returnY + '-' + M + '-' +D; }    //Timestamp of the day (in milliseconds)    varTimestampofday = 1000*60*60*24; //Today, yesterday    varFulltoday =getfulldate (); varFullyesterday = Getfulldate (Nowdate-timestampofday); varNowday = Nowdate.getday ();//The GetDay method returns 0 for SundayNowday = Nowday = = = 0? 7: Nowday; //this Monday, the end of the week (Sunday)    //Note: Under Safari (Nowdate +-0) is not converted to a timestamp, where the operator + is preceded by the nowdate, and the timestamp operation is manually converted    varFullmonday = Getfulldate (+nowdate-(nowDay-1) *timestampofday); varFullsunday = getfulldate (+nowdate + (7-nowday) *timestampofday); //Early month, month end    varFullstartofmonth = Getfulldate (clonenowdate.setdate (1) ); varFullendofmonth =getfulldate (Clonenowdate.setdate (endOfMonth)); return{fulltoday:fulltoday, Fullyesterday:fullyesterday, Fullmonday:fullmonday, Fullsunday: Fullsunday, Fullstartofmonth:fullstartofmonth, fullendofmonth:fullendofmonth};} ());
Second, the package is a module, can be used for webpack tool packaging
varNowdate =NewDate ();varClonenowdate =NewDate ();varFullyear =nowdate.getfullyear ();varmonth = Nowdate.getmonth () + 1;//the GetMonth method returns 0-11, representing January-DecembervarDate =nowdate.getdate ();varEndOfMonth =NewDate (fullyear, month, 0). GetDate ();//get the last day of the month//formatted Date (2016-02-14)functiongetfulldate (targetdate) {vard, Y, M, D; if(targetdate) {D=NewDate (targetdate); Y=d.getfullyear (); M= D.getmonth () + 1; D=d.getdate (); } Else{y=fullyear; M=month; D=date; } m= m > 9? M: ' 0 ' +m; D= d > 9? D: ' 0 ' +D; returnY + '-' + M + '-' +D;}//Timestamp of the day (in milliseconds)varTimestampofday = 1000*60*60*24;//Today, yesterdayvarFulltoday =getfulldate ();varFullyesterday = Getfulldate (Nowdate-timestampofday);varNowday = Nowdate.getday ();//The GetDay method returns 0 for SundayNowday = Nowday = = = 0? 7: Nowday;//this Monday, the end of the week (Sunday)//Note: Under Safari (Nowdate +-0) is not converted to a timestamp, where the operator + is preceded by the nowdate, and the timestamp operation is manually convertedvarFullmonday = Getfulldate (+nowdate-(nowDay-1) *timestampofday);varFullsunday = getfulldate (+nowdate + (7-nowday) *timestampofday);//Early month, month endvarFullstartofmonth = Getfulldate (clonenowdate.setdate (1) );varFullendofmonth =getfulldate (Clonenowdate.setdate (EndOfMonth)); Module.exports={fulltoday:fulltoday, Fullyesterday:fullyesterday, Fullmonday:fullmonday, Fullsunday:fullsunday, Fullstartofmonth:fullstartofmonth, fullendofmonth:fullendofmonth};

Summary: code in two difficult points, one is to get the last day of the month's date, the second is to ask for a week's start and end date time, the 1th is not aware of the problem, the 2nd is compared today with the weekend (Sunday), Monday difference days, plus and minus the time stamp to obtain the corresponding date, in short said difficult, It is not difficult to say that it is possible to spend some time in your busy schedule and keep a clear mind;

Reprint please indicate the Source: Blog Park (Yang Junhua's blog)

JavaScript Date collection (Today, yesterday, Monday, weekend, early month, month end)

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.