RPM1984
RPM1984

Reputation: 73113

How to add a number of days to UTC now in JS?

I've read a bunch of the related questions but just can't figure this one out.

This is what i have:

var days = "10"; // input is in string
var now = new Date();
var nowUtc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
var newDate = nowUtc.setDate(nowUtc.getDate() + parseInt(days));

The values just don't seem correct so i must be doing it wrong. Can anyone help?

I want the value in the end to be ISO 8601 format.

Thanks!

Upvotes: 2

Views: 854

Answers (2)

Mohayemin
Mohayemin

Reputation: 3870

setDate changes the Date object itself. you can use nowUtc.toISOString() to get ISO 8601 string.

nowUtc.setDate(nowUtc.getDate() + parseInt(days));
var iso = nowUtc.toISOString();

Upvotes: 2

xShivan
xShivan

Reputation: 91

Just use moment.js - a library which simplifies DateTime manipulation in JavaScript.

What you are looking for is Add function and String function.

I hope it helps you!

Upvotes: -1

Related Questions