0

I a trying to save a date to the nextMonth. For that I am first setting the month to next 30 days. But the final output date it is giving me in milliseconds. I want the date in GMT format strictly.

What can I do for that?

var snm = new Date();
snm = snm.setDate(snm.getDate() + 30);
                console.log("snm = "+ snm);
Biku7
  • 450
  • 6
  • 16
  • @appleapple no. OP the thing is you're using the date in an expression. If you want the date as a string, use `.toLocaleString()` or another similar API. Also, if you want to go to next month, it'd make sense to call `.getMonth()` and add 1. – Pointy Jan 28 '21 at 16:33
  • 2
    you shouldn't need to reassign `snm`. `snm = snm.setDate` seems wrong here – evolutionxbox Jan 28 '21 at 16:34
  • Understand that a Date instance is not "in" any particular format other than a Date instance. Converting the date to a string by any means is how any formatting is achieved, but it has nothing to do with the intrinsic nature of the object. – Pointy Jan 28 '21 at 16:35
  • The *set* methods return the modified time value, not the Date the method was called on. You must do it in two steps: `snm.setDate(snm.getDate() + 30); console.log(snm.toISOString());` or you can create a new Date object (wasteful, but not much): `new Date(snm.setDate(snm.getDate() + 30)).toISOString()`. – RobG Jan 28 '21 at 23:53
  • Oh, GMT isn't a format, it's a timezone. Perhaps you meant ISO 8601 format like "2021-01-28T23:56:08.062Z". – RobG Jan 28 '21 at 23:56

1 Answers1

-2

Try this

var snm = new Date();
snm.setDate(snm.getDate() + 30)
console.log("snm = "+ snm.toString());
Prabhu
  • 688
  • 5
  • 11