JS Date processing---Calculate the natural month, specify a day of the natural month, get the day before and after, the judgment leap Year, the date of the last day of the month, the last day of the month __js

Source: Internet
Author: User
Tags getdate

Recently by the date made a lot of headaches, because the month of JS is more special [0-11], so in the months of many difficult to calculate. Specially sorted out the date some methods.

First of all, the following code has a common date format function

Date formatted
Date.prototype.format = function (format) {
	var o = {
		"m+": This.getmonth () + 1,//month
		"d+": t His.getdate (),//day
		"h+": this.gethours (),//hour
		"m+": this.getminutes (),//minute
		"s+": This.getsecon DS (),//second
		"q+": Math.floor (This.getmonth () + 3)/3),//Quarter
		"S": This.getmilliseconds ()
	//Milli Second
	}

	if (/(y+)/.test (format))
		format = Format.replace (regexp.$1 () + ""). This.getfullyear (4-regexp.$1.length));
	For (var k in O)
		if (new RegExp ("+ K +")). Test (format)
			format = Format.replace (regexp.$1, Regexp.$1.lengt h = = 1? O[k]: ("+ o[k]"). substr (("" "+ o[k]). length);
	return format;
}

But there is a very annoying thing, that is, JS is not satisfied with the date of the case will automatically add 1 months, such as the current time of March 31, incoming 1, 12 parameters, the expected results for February 29, but the results were exported March 2. is if dissatisfaction will spill over to the next month, then see the API found that the Setmonth has two methods, the other is a specified month, specify a day, you can solve the problem, and extended two methods.

1. Ask for the natural month

I query on the internet three months ago the method, the reference click to open the link http://wawa129.iteye.com/blog/1766263, for transformation, JS code as follows.

Find the natural month date
function getmonthbefore (timetype, num) {
	var date = new Date ();
	var returevalue = "";
	if (TimeType = = 1) {
		date.setmonth (Date.getmonth ()-(num-1));
		Format is a date formatted function, shown below
		Returevalue = Date.format (' yyyy-mm ');
	}
	if (TimeType = = 0) {
		date.setmonth (Date.getmonth ()-(num-1));
		Returevalue = Date.format (' yyyy-mm-01 ');
	}
	return returevalue;
}

Find the natural month Date function Getmonthbefore (TimeType, num) {var date = new Date ();
	var returevalue = "";
		If the input 1 to the output to the month if (TimeType = 1) {Date.setmonth (Date.getmonth ()-(NUM), 1);
	Returevalue = Date.format (' yyyy-mm ');
		} if (TimeType = = 0) {date.setmonth (Date.getmonth ()-(NUM), 1);
	Returevalue = Date.format (' yyyy-mm-01 ');
	}//alert (Returevalue);
return returevalue;
	//Ask for natural month date, add date format symbol function Getmonthbeforeformat (TimeType, Num,format) {var date = new Date ();
	var returevalue = "";
		if (TimeType = = 1) {Date.setmonth (Date.getmonth ()-(NUM), 1);
	Returevalue = Date.format (' yyyy ' +format+ ' MM ');
		} if (TimeType = = 0) {date.setmonth (Date.getmonth ()-(NUM), 1);
	Returevalue = Date.format (' yyyy ' +format+ ' MM ' +format+ ' 01 ');
return returevalue;
    //Ask for natural month date, add date format symbol, specify one day function Getmonthbeforeformatandday (TimeType, num,format,day) {var date = new Date ();
    var returevalue = "";
        if (TimeType = = 1) {Date.setmonth (Date.getmonth ()-(NUM), 1);Returevalue = Date.format (' yyyy ' +format+ ' MM ');
        } if (TimeType = = 0) {date.setmonth (Date.getmonth ()-(NUM), 1);
        The read date is automatically reduced by one, so add one var mo=date.getmonth () +1; To judge if a stupid fork has to be passed a day is greater than the month's maximum date automatically assigned the last day if (mo==4| | mo==6| | mo==9| | mo==11) {if (day>30) {day=30}}else if (mo==2) {if (Isleapyea R (Date.getfullyear ())) {if (day>29) {day=29}else{D
        AY=28}}}else{if (day>31) {day=31}
    } Returevalue = Date.format (' yyyy ' +format+ ' MM ' +format+day);
return returevalue; }

2. Get month End

There are two ways of

Gets the date of the last day of the month function Getlastday (year,month) {var new_year = year;        Take the current year var new_month = month++;//Remove the first day of one months to facilitate calculation (last day is not fixed) if (month>12) {new_month-=12;            month minus new_year++;                Year Increment} var new_date = new Date (new_year,new_month,1); Take the first day of the current month return (new Date (New_date.gettime () -1000*60*60*24)). GetDate ()//Get the date of the last day of the month/** ask for the month end YY
    YY-MM-DD * * @param _month * @returns/function Getmonthendday (_month) {var date = new Date ();
    var day=01;
    var year1 = _month.substr (0,4);
    var month1 = _month.substr (5,2);
        Date.setyear (YEAR1);
        Date.setmonth (month1-1);
        Read date automatically back minus one var mo=date.getmonth () +1; if (mo==4| | mo==6| | mo==9| |
                    mo==11) {day=30}else if (mo==2) {if (Isleapyear ()) { day=29}else{day=28}}else{day=31} date.setdate (parseint (day));
Return Date.format (' Yyyy-mm-dd '); }
3. Get days before and after

function Getdatestr (adddaycount) {
    var dd = new Date ();
    Dd.setdate (Dd.getdate () +adddaycount);//Get Adddaycount days after the date
    var y = dd.getfullyear ();
    var m = dd.getmonth () +1;//Gets the date of the current month
    var d = dd.getdate ();
    Return Dd.format (' Yyyy-mm-dd ');
}

4. Get current date yyyy MM DD

Get the current date yyyy MM DD
function getnowdate (format) {
	var now = new Date ()
	y = now.getfullyear ()
	m = Now.getmo Nth () + 1
	d = now.getdate ()
	m = m < 10? "0" + m:m
	d = d < 10? "0" + d:d return
	y + format + m + format + d
}
5. Judge whether it is a leap year

JS Judge leap Year code
function Isleapyear (year) {if ((
    4) ==0) && ((year% 100)!=0) | | ((year% 400) ==0)) {return
        (true);
    } else {return (false);}
}

The code has done the basic test, just started to write blog, if there are bugs, please advise, also hope that if you have a good deal with the date of the code can also be shared. Thank you.




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.