Dates: Getting Date Parts

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


A Date object offers several methods to check its value. These all depend on the current timezone of the computer:

const date = new Date('July 22, 2018 07:22:13')

date.getDate() //22
date.getDay() //0 (0 means sunday, 1 means monday..)
date.getFullYear() //2018
date.getMonth() //6 (starts from 0)
date.getHours() //7
date.getMinutes() //22
date.getSeconds() //13
date.getMilliseconds() //0 (not specified)
date.getTime() //1532236933000
date.getTimezoneOffset() //-120 (will vary depending on where you are and when you check - this is CET during the summer). Returns the timezone difference expressed in minutes

UTC versions

There are equivalent UTC versions of these methods, that return the UTC value rather than the values adapted to your current timezone:

date.getUTCDate() //22
date.getUTCDay() //0 (0 means sunday, 1 means monday..)
date.getUTCFullYear() //2018
date.getUTCMonth() //6 (starts from 0)
date.getUTCHours() //5 (not 7 like above)
date.getUTCMinutes() //22
date.getUTCSeconds() //13
date.getUTCMilliseconds() //0 (not specified)

Getting the month name

Given a JavaScript Date object instance, how can you get a string that represents the month name?

Every Date object instance has a toLocaleString() method, which is one of the JavaScript internationalization methods.

Using it you can get the month name in your current locale:

const today = new Date()
today.toLocaleString('default', { month: 'long' })

Depending on your current locale you’ll get a different result. I get “October” as a result.

Using the short format for the date, I get “Oct”:

today.toLocaleString('default', { month: 'short' })

The first parameter, to which we pass the default string, is the locale. You can pass any locale you want, for example it-IT will return you ottobre:

const today = new Date()
today.toLocaleString('it-IT', { month: 'long' })

Editing a date

A Date object offers several methods to edit a date value:

const date = new Date('July 22, 2018 07:22:13')

date.setDate(newValue)
date.setFullYear(newValue) //note: avoid setYear(), it's deprecated
date.setMonth(newValue)
date.setHours(newValue)
date.setMinutes(newValue)
date.setSeconds(newValue)
date.setMilliseconds(newValue)
date.setTime(newValue)

setDate and setMonth start numbering from 0, so for example March is month 2.

Example:

const date = new Date('July 22, 2018 07:22:13')

date.setDate(23)
date //July 23, 2018 07:22:13

Fun fact: those methods “overlap”, so if you, for example, set date.setHours(48), it will increment the day as well.

Good to know: you can add more than one parameter to setHours() to also set minutes, seconds and milliseconds: setHours(0, 0, 0, 0) - the same applies to setMinutes and setSeconds.

As for get*, also set* methods have an UTC equivalent:

const date = new Date('July 22, 2018 07:22:13')

date.setUTCDate(newValue)
date.setUTCDay(newValue)
date.setUTCFullYear(newValue)
date.setUTCMonth(newValue)
date.setUTCHours(newValue)
date.setUTCMinutes(newValue)
date.setUTCSeconds(newValue)
date.setUTCMilliseconds(newValue)

Lessons in this unit:

0: Introduction
1: Date Basics
2: ▶︎ Getting Date Parts
3: Date Formatting
4: Date Math
5: Date Comparisons
6: Date Recipes