<p>I need to decrement a Javascript date by 1 day, so that it rolls back across months/years correctly.  That is, if I have a date of 'Today', I want to get the date for 'Yesterday'.</p>

<p>It always seems to take more code than necessary when I do this, so I'm wondering if there's any simpler way.  </p>

<p>What's the simplest way of doing this?</p>

<p>[Edit: Just to avoid confusion in an answer below, this is a <strong>JavaScript</strong> question, not a Java one.]</p>

[{'answer_id': 31938, 'author': 'liammclennan', 'author_id': 2785, 'author_profile': 'https://Stackoverflow.com/users/2785', 'pm_score': 3, 'selected': False, 'text': '<pre><code>var today = new Date();\nvar yesterday = new Date().setDate(today.getDate() -1);\n</code></pre>\n'}, {'answer_id': 31939, 'author': 'Marius', 'author_id': 1585, 'author_profile': 'https://Stackoverflow.com/users/1585', 'pm_score': 6, 'selected': True, 'text': '<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>var d = new Date();\r\nd.setDate(d.getDate() - 1);\r\n\r\nconsole.log(d);</code></pre>\r\n</div>\r\n</div>\r\n</p>\n'}, {'answer_id': 31943, 'author': 'Philip Reynolds', 'author_id': 1087, 'author_profile': 'https://Stackoverflow.com/users/1087', 'pm_score': 2, 'selected': False, 'text': '<p><code>getDate()-1</code> should do the trick</p>\n\n<p>Quick example:</p>\n\n<pre><code>var day = new Date( "January 1 2008" );\nday.setDate(day.getDate() -1);\nalert(day);\n</code></pre>\n'}, {'answer_id': 357000, 'author': 'John Griffiths', 'author_id': 24765, 'author_profile': 'https://Stackoverflow.com/users/24765', 'pm_score': 1, 'selected': False, 'text': '<p><code>setDate(dayValue)</code></p>\n\n<p><code>dayValue</code> is an integer from 1 to 31, representing the day of the month.</p>\n\n<p>from <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date/setDate" rel="nofollow noreferrer">https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date/setDate</a></p>\n\n<p>The behaviour solving your problem (and mine) seems to be out of specification range.</p>\n\n<p>What seems to be needed are addDate(), addMonth(), addYear() ... functions.</p>\n'}, {'answer_id': 20407757, 'author': 'adrian7', 'author_id': 319150, 'author_profile': 'https://Stackoverflow.com/users/319150', 'pm_score': 2, 'selected': False, 'text': '<pre><code>origDate = new Date();\ndecrementedDate = new Date(origDate.getTime() - (86400 * 1000));\n\nconsole.log(decrementedDate);\n</code></pre>\n'}, {'answer_id': 25114400, 'author': 'Sunil B N', 'author_id': 1641714, 'author_profile': 'https://Stackoverflow.com/users/1641714', 'pm_score': 3, 'selected': False, 'text': '<pre><code> day.setDate(day.getDate() -1); //will be wrong\n</code></pre>\n\n<p>this will return wrong day. under UTC -03:00, check for </p>\n\n<pre><code>var d = new Date(2014,9,19);\nd.setDate(d.getDate()-1);// will return Oct 17\n</code></pre>\n\n<p><strong>Better use:</strong></p>\n\n<pre><code>var n = day.getTime();\nn -= 86400000;\nday = new Date(n); //works fine for everything\n</code></pre>\n'}, {'answer_id': 49706911, 'author': 'Aliaksandr Sushkevich', 'author_id': 7600492, 'author_profile': 'https://Stackoverflow.com/users/7600492', 'pm_score': 1, 'selected': False, 'text': '<p>Working with dates in JS can be a headache. So the simplest way is to use <a href="https://momentjs.com/" rel="nofollow noreferrer">moment.js</a> for any date operations.</p>\n\n<p>To subtract one day: </p>\n\n<pre><code>const date = moment().subtract(1, \'day\')\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/31931', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/916/']