propernoun
propernoun

Reputation: 61

format javascript date and add 365 days

I want to add 365 days to a formatted javascript date.

var today = new Date();
var day = today.getDate();
var month = today.getMonth();
var year = today.getFullYear();
today = year +"-"+ day +"-"+ month;
var duedate = new Date(today);
duedate.setDate(today.getDate() + 365);

Console says that today.getDate() in the last line is not a function. How do I correctly add 365 days to the formatted date? Thank you!

Upvotes: 0

Views: 10041

Answers (2)

DDan
DDan

Reputation: 8276

With a Date object you can do that.

var now = new Date();
var duedate = new Date(now);
duedate.setDate(now.getDate() + 365);
console.log("Now:     ", now);
console.log("Due Date:", duedate);

Is it necessary to edit formatted date? In that case you would need to operate with strings/substrings. Not very beautiful approach.

Upvotes: 3

Trishul
Trishul

Reputation: 1028

All you have to do is to remove

today = year +"-"+ day +"-"+ month;

This line converts the date object into string.

Upvotes: 0

Related Questions