4

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.

2
  • 1
    you want to add 365 days to the current date? 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" Commented Mar 22, 2016 at 5:06

3 Answers 3

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();

Sign up to request clarification or add additional context in comments.

5 Comments

What happens in a leap year? ;)
@piyush what should be the output in case of 2016-01-01
output should be 2016-12-31
@piyush check the updated code at the end, it gives the output you are looking for
how can i do it same in php?
2

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'));

Comments

0

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);

1 Comment

@epascarello.. thanks for noticing this. I have edited.. hope the latest answer works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.