JS time to get the day, yesterday, this week, last week, this month, last month

Source: Internet
Author: User

Call

Settimerange (2);

function Settimerange (v) {
var FMT = ' Yyyy-mm-dd hh:mm ';
var now = new Date ();
var end = new Date ();
if (v = = 0) {

} else if (v = = 1) {

} else if (v = = 2) {
Yesterday
Now.setdate (now.getdate ()-1);
End.setdate (end.getdate ()-1);
} else if (v = = 3) {
Week
var Nowdayofweek = Now.getday (); The day of the week today
if (nowdayofweek = = 0) {
Nowdayofweek = 6;//sunday
} else {
Nowdayofweek = nowDayOfWeek-1;
}
Now.setdate (now.getdate ()-nowdayofweek);
End.setdate (now.getdate () + 6);
} else if (v = = 4) {
Last week
var Nowdayofweek = Now.getday (); The day of the week today
if (nowdayofweek = = 0) {
Nowdayofweek = 6;//sunday
} else {
Nowdayofweek = nowDayOfWeek-1;
}

Now.setdate (now.getdate ()-nowDayOfWeek-7);
End.setdate (now.getdate () + 6);
} else if (v = = 5) {
Month
Now.setdate (1);

var nextmonth = End.getmonth () + 1;
var nextmonthfirstday=new Date (end.getfullyear (), nextmonth,1);
var oneday= 1000 * 60 * 60 * 24;
end = new Date (nextmonthfirstday-oneday);
} else if (v = = 6) {
Last month
var nextmonth = Now.getmonth ()-1;
Now.setmonth (nextmonth);
Now.setdate (1);

Nextmonth = Nextmonth + 1;
var nextmonthfirstday= new Date (end.getfullyear (), nextmonth, 1);
var oneday= 1000 * 60 * 60 * 24;
end = new Date (nextmonthfirstday-oneday);
}

Now.sethours (0);
Now.setminutes (0);
Now.setseconds (0);
End.sethours (23);
End.setminutes (59);
End.setseconds (59);

Let's print this out here.
Console.log (now + ': ' + end);
}

JS time to get the day, yesterday, this week, last week, this month, last month

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.