Skip to content

Instantly share code, notes, and snippets.

@mribbons
Created October 31, 2024 05:46
Show Gist options
  • Select an option

  • Save mribbons/7bd3e4f9eb9a5fe733d3bd6ff3eff4fc to your computer and use it in GitHub Desktop.

Select an option

Save mribbons/7bd3e4f9eb9a5fe733d3bd6ff3eff4fc to your computer and use it in GitHub Desktop.
let [ year, month, day ] = [2024, 9, 12]
let scaryDate = new Date()
scaryDate.setFullYear(year)
scaryDate.setMonth(month - 1) // set month input is zero based
scaryDate.setDate(day)
console.log(`πŸ¦‡`, scaryDate, `πŸ¦‡`)
// output:
// πŸ¦‡ 2024-10-12T05:36:21.947Z πŸ¦‡
// October?? let's take a look at the setMonth docs: (emojis added for effect)
/**
* Sets the month value in the Date object using local time.
* @param month A numeric value equal to the month. The value for January is 0, and other month values follow consecutively.
* @param date A numeric value representing the day of the month. πŸŽƒπŸŽƒπŸŽƒ If this value is not supplied, the value from a call to the getDate method is used πŸŽƒπŸŽƒπŸŽƒ.
*/
let goodDate = new Date()
goodDate.setFullYear(year)
goodDate.setMonth(month - 1, day) // provide the date as well, just as you would completely not expect.
goodDate.setDate(day)
console.log(`πŸ˜…`, goodDate, `πŸ˜…`)
// output:
// πŸ˜… 2024-09-12T06:36:21.947Z πŸ˜…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment