-1

I have this String 29/06/2017i want to convert it into JavaScript date and add one year to it results Should be 28/06/2018 how to do it . i have tried this

var MyDate = new Date('29/06/2017');

Output is invalid date object how to convert it and add one year.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
bharathRaj
  • 25
  • 8

4 Answers4

0

Unlike the other users, i would like to let you know about another way to approach this issue.

Moment.js

Documentation - Reference

What's the purpose ?

This library is very popular among Javascript Developer because it is easy to implement and allows you to manipulate Date way more easily with provided built-in features.

Also, another interesting problem that is solved by using this library is the internationalization.

Internationalization and Date

As you may know, every language has its own way to handle and write date. (Translated Month, Different Display, ect).

Moment.js provide a full suite of feature that allows you to deal with these easily with pre-translated words and syntax.

How to fix your issue with Moment.js

var myDate = moment("29/06/2017", "MM/DD/YYYY");

Furthermore you can check if your Date is valid

myDate.isValid();

And here, you can simply add a year by using :

myDate.add(1, 'y'); // Which stands for add a year
Louis Lecocq
  • 1,764
  • 3
  • 17
  • 38
0

This is two questions on one. The parsing part is answered by Why does Date.parse give incorrect results. Once you have a Date, adding a year is straight forward but you need to account for adding a year to 29 February, which will return 1 March (since there will not be a 29 February the following year).

// Simple parser for DD/MM/YYYY
function parseDMY(s) {
  var b = s.split(/\D/);
  return new Date(b[2], b[1]-1, b[0]);
}

// Simple formatter: Date to DD/MM/YYYY
function formatDMY(d) {
  return ('0' + d.getDate()).slice(-2) + '/' +
    ('0' + (d.getMonth()+1)).slice(-2) + '/' +
    d.getFullYear();
}

// Add year to a Date
function addYear(d) {
  var date = d.getDate();
  d.setFullYear(d.getFullYear() + 1);
  // Test if 29 Feb rolled over to 1 Mar
  if (date != d.getDate()) {
    d.setDate(d.getDate() - 1);
  }
  return d;
}

// Tests (minimal)
['29/02/2016','29/6/2017'].forEach(function(s){
  var date = parseDMY(s);
  addYear(date);
  console.log(formatDMY(date));
});

If you want the previous day (e.g. 30/06/2016 -> 29/06/2017) then you need to subtract a day after adding the year, which is the same as this line:

d.setDate(d.getDate() - 1);

or you can modify the addYear function to do it for you.

RobG
  • 142,382
  • 31
  • 172
  • 209
-1

You can try to switch the Day and the Year

var date = new Date('29/06/2017'.split('/').reverse().join('/'));
var plusOneYear = date.setFullYear(date.getFullYear()+1);
console.log(date); // output: Fri Jun 29 2018 00:00:00
console.log(new Date(plusOneYear)); // output: Fri Jun 29 2018 00:00:00
console.log(date.toLocaleDateString()) // output: 6/29/2018
console.log(date.getDate() + '/' + (date.getMonth+1) + '/' + date.getFullYear()); // output: 29/6/2018

another way is

var date = '29/06/2017'.split('/'); // will become [day][month][year]
var newDate = date[2] + '/' + date[1] + '/' + date[0]; // join in format you would prefer
var plusOneYear = newDate.setFullYear(newDate.getFullYear()+1);
console.log(new Date(plusOneYear));
console.log(newDate.toLocaleDateString()) // output: 6/29/2018

Edited

Yordan Nikolov
  • 2,598
  • 13
  • 16
  • thank you its working but how can i add one year to that date and again format it to this formate 28/06/2018 – bharathRaj Jun 30 '17 at 12:02
  • Please do not parse strings with the built-in parser. Also, the OP wants the day before. Have you tried this with 29/02/2016? – RobG Jul 01 '17 at 21:39
-1

Try this,

var MyDate = new Date('29/06/2017'.split('/').reverse().join('/')); 
new Date(MyDate.setYear(MyDate.getFullYear() + 1));
Kaustubh Khare
  • 3,280
  • 2
  • 32
  • 48