最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to Add 1 year to startdate to get the end date in javascript - Stack Overflow

programmeradmin4浏览0评论

want a output like below

if Start Date is "01-03-2016" the End date should be "28-02-2017" if Start Date is "10-04-2016" the End date should be "09-04-2017"

I tried below code

if (dat <= 31 && dat >= 1 && month <= 12 && month >= 1) {

            var expiryDate = new Date(n1, month - 1, dat);

            expiryDate.setFullYear(expiryDate.getFullYear() + 1);
            var day = ('0' + expiryDate.getDate()).slice(-2);
            var month1 = ('0' + (expiryDate.getMonth() + 1)).slice(-2);
            var year = expiryDate.getFullYear();
            var month = getMonthName(month1);

            var wholeenddate = day + "-" + month + "-" + year;

but it's not produce desired output.Please Help to solve it.

want a output like below

if Start Date is "01-03-2016" the End date should be "28-02-2017" if Start Date is "10-04-2016" the End date should be "09-04-2017"

I tried below code

if (dat <= 31 && dat >= 1 && month <= 12 && month >= 1) {

            var expiryDate = new Date(n1, month - 1, dat);

            expiryDate.setFullYear(expiryDate.getFullYear() + 1);
            var day = ('0' + expiryDate.getDate()).slice(-2);
            var month1 = ('0' + (expiryDate.getMonth() + 1)).slice(-2);
            var year = expiryDate.getFullYear();
            var month = getMonthName(month1);

            var wholeenddate = day + "-" + month + "-" + year;

but it's not produce desired output.Please Help to solve it.

Share Improve this question asked Mar 22, 2016 at 5:01 piyushpiyush 3211 gold badge7 silver badges21 bronze badges 2
  • 1 you want to add 365 days to the current date? – gurvinder372 Commented Mar 22, 2016 at 5:03
  • If i add 365 days then in case if my date is "01-03-2016" then it will give end date "01-03-2017" – piyush Commented Mar 22, 2016 at 5:06
Add a ment  | 

3 Answers 3

Reset to default 4

Add 364 days to your date

For example

var d = new Date("2016-03-01");
d.setDate(d.getDate()+364); //outputs 28-02-2017

and

var d = new Date("2016-04-10");
d.setDate(d.getDate()+364); //outputs 09-04-2017

or Just add 1 year and sub 1 day.

d.setFullYear(d.getFullYear() + 1);
d.setDate(d.getDate()-1);

Now it will match your output just the same even for leap year :)

Demo

var d = new Date("2016-03-01");
d.setFullYear(d.getFullYear() + 1);
d.setDate(d.getDate()-1);
document.body.innerHTML += d.toString();

document.body.innerHTML += "<br>";


d = new Date("2016-04-10");
d.setFullYear(d.getFullYear() + 1);
d.setDate(d.getDate()-1);
document.body.innerHTML += d.toString();

There's a convenient library to help with this sort of thing - moment.js (14k zipped).

var startDate = moment('01-03-2016', 'DD-MM-YYYY');
console.log(startDate.format('DD-MM-YYYY'));
var endDate = startDate.clone();
endDate.add(1, 'years').subtract('1', 'days');
console.log(endDate.format('DD-MM-YYYY'));

3 ways to do this.

 // Add hours
 var today = new Date();
 today.setHours(today.getHours()+24*364);

 // Add days
 var nextyearDate = new Date();
 nextyearDate.setDate(today.getDate()+364);

 // More reliable way : Add year & subtract a day.. Hope this works...! Works for 01/01/2016
 var nextDate = new Date();
 nextDate.setYear(nextDate.getFullYear()+1);
 nextDate.setDate(nextDate.getDate()-1);
发布评论

评论列表(0)

  1. 暂无评论