JS Date Calculation and quick get week, month, quarter start and end days, get a small example of the week of the specified date and the day of the week

Source: Internet
Author: User
Tags null null

JS Gets the date when it encounters the following requirements, and gets the day of the week based on the week of the year. If the start date is from Thursday to the next week Friday is one week.

Formatted Date:

functiongetnowformatdate (thedate) {varDay =thedate;varYear = 0; varMonth = 0; varDay = 0; varcurrentdate = ""; //Initialization TimeYear= day.getfullyear ();//ie Firefox can beMonth= Day.getmonth () +1; Day=day.getdate (); currentdate+ = year + "-"; if(Month >= 10) {currentdate+ = Month + "-"; } Else{currentdate+ = "0" + Month + "-"; } if(Day >= 10) {currentdate+=Day ;} Else{currentdate+ = "0" +Day ;} returncurrentdate;}

Get Date Range Display

functionvar var if null null | | _week = = "return "= getxdate (_year,_week,4= Getxdate (_year, (_week-0 + 1), 5return getnowformatdate (begindate) + "to" + getnowformatdate (endDate);}  

This method will get the day of the Week (WeekDay) of the first week (weeks) of the year

functiongetxdate (year,weeks,weekday) {//constructs a Date object with the specified year and sets the date to January 1 of the year//because the month in the computer starts with 0, the following construction methods arevarDate =NewDate (year, "0", "1"); //gets the long shaping time of this date object datevarTime =Date.gettime (); //Add This long shaping time to the nth week time offset//because the first week is the current week, so there are: weeks-1, etc.//7*24*3600000 is the time of the week in milliseconds, (the date in JS is accurate to milliseconds)time+= (weeks-1) *7*24*3600000; //Reset the Date object date to timeDate.settime (time);returngetnextdate (date,weekday);} 

This method will get the date of the week (WeekDay) of the week in which a date (nowdate) is located.

function//weekday%=7var day =var time =   var sub = weekday-if(sub <= 0+ = 7;}time +=sub*24*3600000  return  nowdate;}

JS Gets the date of the current specified previous days
1 functiongetbeforedate (n) {2     varn =N;3     varD =NewDate ();4     varYear =d.getfullyear ();5     varMon=d.getmonth (+1);6     varday=d.getdate ();7     if(Day <=N) {8             if(mon>1) {9Mon=mon-1;Ten             } One            Else { AYear = Year-1; -Mon = 12; -              } the            } -D.setdate (D.getdate ()-n); -Year =d.getfullyear (); -Mon=d.getmonth (+1); +day=d.getdate (); -s = year+ "-" + (mon<10? (' 0 ' +mon): Mon) + "-" + (day<10? (' 0 ' +Day ):d ay); +      returns; A } atConsole.log (Getbeforedate (1));//Yesterday's date -Console.log (Getbeforedate (7));//date of first seven days

JS Date Calculation and quick get week, month, quarter start and end days, get a small example of a specified date week and day of the week

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.