Calculate months in Javascript

1

Any way to calculate 4 exact months after a date obtained from a string, for example "17/12/2017"?

Thanks in advance!

    
asked by mslash 15.06.2017 в 18:25
source

1 answer

1

You have two problems:

Transform a string into a String

For your example, you could use something like this. Simple and specific. If you want something more generic you should go to some kind of solution by library.

var strDate = "17/12/2017";
var dateParts = strDate.split("/");

var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);

Add months to a date

Here is an example that adds three months to the date January 31, 2009

var jan312009 = new Date(2009, 0, 31);
var eightMonthsFromJan312009  = jan312009.setMonth(jan312009.getMonth()+3);

Greetings

    
answered by 15.06.2017 в 18:32