This article example describes the method of JavaScript perfectly implementing a given date to return the date of the previous month. Share it for your reference, as follows:
In project development, when using javascript to process dates, there will be an initial value in the query, and most of them will be pushed for a month based on the current date. In this case, if you write one by yourself, there are many situations to consider. Here I will share with you a relatively complete solution to this problem. For your reference. For example: given deadline enddate=2010-07-31
Calculate the start date startdate=2010-06-30
The key to this problem lies in the considerations of the following:
1. Startdate New Year's Eve
2. Startdate is February (the situation of leap year needs to be considered)
3. Big and small months
<html> <script type="text/javascript"> function getInitStartDate(enddate) { var comp = ("-"); var year = comp[0]; var month = comp[1]; var date = comp[2]; if (month == "01") { //The New Year's Eve the previous month month = 12; year = year - 1; } else { month = month - 1; if (month == 2 && date > 28) { date = isLeapYear(year) ? 29 : 28; } else if (date == 31) { switch (month) { case 4: case 6: case 9: case 11: date = 30; break; default: break; } } } month = ("" + month).length == 1 ? ("0" + month) : ("" + month); var dateFormat = year + "-" + month + "-" + date; return dateFormat; } function isLeapYear(y) { //Judge whether y is a leap year return (y % 4 == 0) && (y % 400 == 0 || y % 100 != 0); } alert(getInitStartDate("2010-07-31")); </script> </html>
A method to format the date is given in conjunction:
<script language="JavaScript"> = function(format) //author: meizz { var o = { "M+" : ()+1, //month "d+" : (), //day "h+" : (), //hour "m+" : (), //minute "s+" : (), //second "q+" : ((()+3)/3), //quarter "S" : () //millisecond } if(/(y+)/.test(format)) format=(RegExp.$1, (()+"").substr(4 - RegExp.$)); for(var k in o)if(new RegExp("("+ k +")").test(format)) format = (RegExp.$1, RegExp.$==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); return format; } alert(new Date().format("yyyy-MM-dd")); alert(new Date("january 12 2008 11:12:30").format("yyyy-MM-dd hh:mm:ss")); </script>
PS: Here are a few more practical online tools for daily calculation for everyone to use:
Online Date/Day Calculator:
http://tools./jisuanqi/date_jisuanqi
Online date calculator/phase difference day calculator:
http://tools./jisuanqi/datecalc
Online date day difference calculator:
http://tools./jisuanqi/onlinedatejsq
Online Day Calculator:
http://tools./jisuanqi/datejsq
For more information about JavaScript, readers who are interested in reading this site's special topic:Summary of JavaScript time and date operation skills》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《JavaScript traversal algorithm and skills summary"and"Summary of JavaScript mathematical operations usage》
I hope this article will be helpful to everyone's JavaScript programming.