• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Time & Date / Date of yesterday

Date of yesterday

Date of yesterday

Results in a string representation of yesterday’s date.

  • Use the Date constructor to get the current date.
  • Decrement it by one using Date.prototype.getDate() and set the value to the result using Date.prototype.setDate().
  • Use Date.prototype.toISOString() to return a string in yyyy-mm-dd format.
const yesterday = () => {
  let d = new Date();
  d.setDate(d.getDate() - 1);
  return d.toISOString().split('T')[0];
};

// EXAMPLE
yesterday(); // 2018-10-17 (if current date is 2018-10-18)

Source

https://www.30secondsofcode.org/js/s/yesterday

Time & Date

Related Snippets:

  • Check if a date is the same as another date
  • Check if a date is after another date
  • Get the amount of time from now for a date
  • Check if a date is between two other dates

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2023 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us