Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
153
rated 0 times [  156] [ 3]  / answers: 1 / hits: 40546  / 8 Years ago, fri, july 8, 2016, 12:00:00

I need to get today's date -30 days but in the format of: 2016-06-08



I have tried setDate(date.getDate() - 30); for -30 days.



I have tried date.toISOString().split('T')[0] for the format.



Both work, but somehow cannot be used together.


More From » javascript

 Answers
13

setDate() doesn't return a Date object, it returns the number of milliseconds since 1 January 1970 00:00:00 UTC. You need separate calls:



var date = new Date();
date.setDate(date.getDate() - 30);
var dateString = date.toISOString().split('T')[0]; // 2016-06-08

[#61451] Thursday, July 7, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cadendericki

Total Points: 482
Total Questions: 109
Total Answers: 103

Location: Ecuador
Member since Thu, Jun 4, 2020
4 Years ago
cadendericki questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Wed, Jul 8, 20, 00:00, 4 Years ago
Thu, May 14, 20, 00:00, 4 Years ago
;