简体   繁体   English

如何在javascript中给定日期添加月份数?

[英]How To add number of month into the given date in javascript?

I want to add month into the select date by the user. 我想将月份添加到用户的选择日期中。

startdate=document.getElementById("jscal_field_coverstartdate").value;

now I want to add 11 month from the above startdate. 现在我想从上述开始日期开始添加11个月。 How to do that. 怎么做。

date format = 2013-12-01 日期格式= 2013-12-01

Without the date format it is difficult to tell, however you can try like this 没有日期格式,很难分辨,但是您可以这样尝试

add11Months = function (date) {
    var splitDate = date.split("-");
    var newDate = new Date(splitDate[0], splitDate[1] - 1, splitDate[2]);
    newDate.setMonth(newDate.getMonth() + 11);
    splitDate[2] = newDate.getDate();
    splitDate[1] = newDate.getMonth() + 1;
    splitDate[0] = newDate.getFullYear();
    return startdate = splitDate.join("-");
}

var startdate = add11Months("2013-12-01");
alert(startdate)

JSFiddle JSFiddle

If your startdate is in correct date format you can try using moment.js or Date object in javascript. 如果您的startdate是在正确的日期格式,你可以尝试使用moment.jsDate对象的JavaScript。
In Javascript, it can be achieved as follow: 在Javascript中,可以通过以下方式实现:

var date = new Date("2013-12-01");
console.log(date);  
//output: Sun Dec 01 2013 05:30:00 GMT+0530 (India Standard Time)
var newdate = date.setDate(date.getDate()+(11*30));  
console.log(new Date(newdate));  
// output: Mon Oct 27 2014 05:30:00 GMT+0530 (India Standard Time)  

In above lines, I have used 30 days per month as default. 在以上几行中,我默认每月使用30天。 So you will get exact 11 month but little deviation in date. 因此,您将获得准确的11个月但日期偏差很小。 Is this what you want ? 这是你想要的吗 ? You can play around this likewise. 您也可以这样做。 I hope it help :) 希望对您有所帮助:)
For more about Date you can visit to MDN . 有关日期的更多信息,请访问MDN

You can do it like this: 您可以这样做:

var noOfMonths = 11
var startdate = document.getElementById("jscal_field_coverstartdate").value;
startdate.setMonth(startdate.getMonth() + noOfMonths)

Try this: 尝试这个:

baseDate.setMonth(2);    
baseDate.setDate(30);
noMonths = 11;
var sum = new Date(new Date(baseDate.getTime()).setMonth(baseDate.getMonth() + noMonths);
if (sum.getDate() < baseDate.getDate()) {  sum.setDate(0);  }
var m = newDate.getDate();
var d =  newDate.getMonth() + 1;
var yyyy = newDate.getFullYear();
return (yyyy+"-"+m+"-"+d);

Notes: 笔记:

Adding months (like adding one month to January 31st) can overflow the days field and cause the month to increment (in this case you get a date in March). 添加月份(例如在1月31日之前添加一个月)可能会使days字段溢出,并导致月份递增(在这种情况下,您会获得3月的日期)。 If you want to add months and then overflow the date then .setMonth(base_date.getMonth()+noMonths) works but that's rarely what people think of when they talk about incrementing months. 如果要添加月份然后溢出日期,则.setMonth(base_date.getMonth()+ noMonths)可以工作,但是当人们谈论增加月份时,这很少有人想到。 It handles cases where 29, 30 or 31 turned into 1, 2, or 3 by eliminating the overflow Day of Month is NOT zero-indexed so .setDate(0) is last day of prior month. 它通过消除溢出的Month Day不是零索引来处理29、30或31变成1、2或3的情况,因此.setDate(0)是上个月的最后一天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM