Javascript perfectly implements the method to return the last month's date for a given date. javascript returns the last month's date

Source: Internet
Author: User

Javascript perfectly implements the method to return the last month's date for a given date. javascript returns the last month's date

This example describes how javascript perfectly returns the last month's date from a given date. We will share this with you for your reference. The details are as follows:

In project development, when javascript is used to process a date, because there is an initial value in the query, most of them will be pushed for one month based on the current date. In this case, if you write one by yourself, you need to consider a lot of situations. Here we will share with you a more complete solution to this problem. For your reference. Example: enddate =

Startdate =

The key to this problem lies in the following considerations:

1. startdate

2. startdate is January 1, February (the leap year needs to be considered)

3. Month

<Html> <script type = "text/javascript"> function getInitStartDate (enddate) {var comp = enddate. split ("-"); var year = comp [0]; var month = comp [1]; var date = comp [2]; if (month = "01 ") {// the previous month across the year 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) {// determines whether y is a leap year return (y % 4 = 0) & (y % 400 = 0 | y % 100! = 0);} alert (getInitStartDate ("2010-07-31"); </script> 

The following describes how to format the date:

<script language="JavaScript"> Date.prototype.format = function(format) //author: meizz{ var o = {  "M+" : this.getMonth()+1, //month  "d+" : this.getDate(),  //day  "h+" : this.getHours(),  //hour  "m+" : this.getMinutes(), //minute  "s+" : this.getSeconds(), //second  "q+" : Math.floor((this.getMonth()+3)/3), //quarter  "S" : this.getMilliseconds() //millisecond } if(/(y+)/.test(format)) format=format.replace(RegExp.$1,  (this.getFullYear()+"").substr(4 - RegExp.$1.length)); for(var k in o)if(new RegExp("("+ k +")").test(format))  format = format.replace(RegExp.$1,   RegExp.$1.length==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 some useful online days computing tools for you to use:

Online date/day calculator:
Http://tools.jb51.net/jisuanqi/date_jisuanqi

Online date calculator/days calculator:
Http://tools.jb51.net/jisuanqi/datecalc

Online date-days difference calculator:
Http://tools.jb51.net/jisuanqi/onlinedatejsq

Online days calculator:
Http://tools.jb51.net/jisuanqi/datejsq

Related Article

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.