Skip to content

Instantly share code, notes, and snippets.

@cp-sumi-k
Last active November 8, 2022 09:51
Show Gist options
  • Save cp-sumi-k/e9b4b804c86f3b96da6c8788ea4cc206 to your computer and use it in GitHub Desktop.
Save cp-sumi-k/e9b4b804c86f3b96da6c8788ea4cc206 to your computer and use it in GitHub Desktop.
/** year **/
// Current year : 2022
console.log("Current year : ", new Date().getFullYear())
// Given date year : 2018
console.log("Given date year : ", new Date("2018-03-25").getFullYear())
/**
month
getMonth() starts with 0, it will returns currentMonth - 1 number (11 - 1 = 10).
**/
// Current month : 10
console.log("Current month : ", new Date().getMonth())
// Given date month : 2
console.log("Given date month : ", new Date("2018-03-25").getMonth())
// Full month name : November
// For short name, can use 'short'
console.log("Full month name : ", cDate.toLocaleString('default', { month: 'long' }))
/** date **/
// Current date : 7
console.log("Current date : ", new Date().getDate())
// Given date's date : 25
console.log("Given date's date : ", new Date("2018-03-25").getDate())
/** weekday **/
// Today's weekday : 1 (monday)
console.log("Today's weekday : ", new Date().getDay())
// Given date weekday : 0 (sunday)
console.log("Given date's date : ", new Date("2018-03-25").getDay())
/** millis **/
// Current time in millis : 1667813042954
console.log("Current time in millis : ", Date.now())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment