JS gets a week, month, next month, a quarter of the start date, the end date and the judgment date of the week

Source: Internet
Author: User

Formatted Date: YYYY-MM-DD

function FormatDate (date) {
var myyear = Date.getfullyear ();
var mymonth = Date.getmonth () +1;
var myweekday = Date.getdate ();

if (Mymonth < 10) {
Mymonth = "0" + mymonth;
}
if (Myweekday < 10) {
Myweekday = "0" + myweekday;
}
Return (myyear+ "-" +mymonth + "-" + myweekday);
}

Get the number of days in a month
function Getmonthdays (parayear,paramonth) {
var monthstartdate = new Date (Parayear, Paramonth, 1);
var monthenddate = new Date (parayear, Paramonth + 1, 1);
var days = (monthenddate-monthstartdate)/(1000 * 60 * 60 * 24);
return days;
}

Get the start date for a week
function Getweekstartdate (parayear,paramonth,paraday,paradayofweek) {
var weekstartdate = new Date (parayear, Paramonth, Paraday + 1-paradayofweek);
return FormatDate (weekstartdate);
}

Get the end date of a week
function Getweekenddate (parayear,paramonth,paraday,paradayofweek) {
var weekenddate = new Date (parayear, Paramonth, Paraday + (7-paradayofweek));
return FormatDate (weekenddate);
}

Get the start date of a month
function Getmonthstartdate (parayear,paramonth) {
var monthstartdate = new Date (Parayear, Paramonth, 1);
return FormatDate (monthstartdate);
}

Get the end date of a month
function Getmonthenddate (parayear,paramonth) {
var monthenddate = new Date (Parayear,paramonth, Getmonthdays (Parayear,paramonth));
return FormatDate (monthenddate);
}

Get start time last month
function Getlastmonthstartdate (parayear, Lastmonth) {
var lastmonthstartdate = new Date (Parayear, Lastmonth, 1);
return FormatDate (lastmonthstartdate);
}

Get last month end time
function Getlastmonthenddate (parayear, Lastmonth, Lastmonth) {
var lastmonthenddate = new Date (parayear, Lastmonth, Getmonthdays (Lastmonth));
return FormatDate (lastmonthenddate);
}

Get the start date for a quarter
function Getquarterstartdate (Parayear,paraseason) {
Switch (Paraseason) {
Case ' 1 ': Return parayear+ "-01-01";
Case ' 2 ': Return parayear+ "-04-01";
Case ' 3 ': Return parayear+ "-07-01";
Case ' 4 ': Return parayear+ "-10-01";
}
}

Get the end date for a quarter
function Getquarterenddate (Parayear,paraseason) {
Switch (Paraseason) {
Case ' 1 ': Return parayear+ "-03-31";
Case ' 2 ': Return parayear+ "-06-30";
Case ' 3 ': Return parayear+ "-09-30";
Case ' 4 ': Return parayear+ "-12-31";
}
}

Get the start date of a week of a year
function Getbegindateofweek (parayear, Weekindex) {
var firstday = Getfirstweekbegday (parayear);
7*24*3600000 is the time of the week in milliseconds, (the date in JS is accurate to milliseconds)
var time= (weekIndex-1) *7*24*3600000;
var beginday = FirstDay;
Reset the Date object date to time
Beginday.settime (firstday.valueof () +time);
return FormatDate (Beginday);
}

Get the end date of a week of a year
function Getenddateofweek (parayear, Weekindex) {
var firstday = Getfirstweekbegday (parayear);
7*24*3600000 is the time of the week in milliseconds, (the date in JS is accurate to milliseconds)
var time= (weekIndex-1) *7*24*3600000;
var weektime = 6*24*3600000;
var endday = FirstDay;
Reset the Date object date to time
Endday.settime (firstday.valueof () +weektime+time);
return FormatDate (Endday);
}

Get date is the week ordinal of a year
function Getweekindex (dateobj) {
var firstday = Getfirstweekbegday (Dateobj.getfullyear ());
if (Dateobj < FirstDay) {
FirstDay = Getfirstweekbegday (Dateobj.getfullyear ()-1);
}
D = Math.floor ((dateobj.valueof ()-firstday.valueof ())/86400000);
return Math.floor (D/7) + 1;
}

Get the first day of a year
function Getfirstweekbegday (year) {
var tempdate = new Date (year, 0, 1);
var temp = Tempdate.getday ();
if (temp = = 1) {
return tempdate;
}
temp = Temp = = 0 7:temp;
Tempdate = Tempdate.setdate (Tempdate.getdate () + (8-temp));
return new Date (tempdate);
}

JS gets a week, month, next month, a quarter of the start date, the end date and the judgment date 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.