Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Reading Date and Time Values | Working with Dates in JavaScript
Working with Dates and Times in JavaScript

bookReading Date and Time Values

JavaScript provides several methods on the Date object to extract specific components of a date and time. Use the following methods to access these values:

  • getFullYear(): returns the four-digit year;
  • getMonth(): returns the zero-based month index (0 for January, 11 for December);
  • getDate(): returns the day of the month;
  • getHours(): returns the hour of the day (0–23);
  • getMinutes(): returns the minutes (0–59);
  • getSeconds(): returns the seconds (0–59).

These methods allow you to break down a date into its key components for display, calculation, or logic in your application.

123456789
const today = new Date(); const year = today.getFullYear(); const month = today.getMonth(); // Zero-based: January is 0 const day = today.getDate(); console.log("Year:", year); console.log("Month:", month); console.log("Day:", day);
copy
123456789
const now = new Date(); const hours = now.getHours(); const minutes = now.getMinutes(); const seconds = now.getSeconds(); console.log("Hours:", hours); console.log("Minutes:", minutes); console.log("Seconds:", seconds);
copy

The getMonth() method returns a zero-based index for months, meaning January is represented as 0, February as 1, and December as 11. This can lead to confusion if you expect months to start at 1. In the previous code sample, if getMonth() returns 5, it actually refers to June, not May. When displaying or processing month values, add 1 to the result to match common expectations and avoid off-by-one errors.

To read the day of the week from a date, use the getDay() method. This method returns an integer between 0 and 6, where 0 represents Sunday and 6 represents Saturday. Reading the weekday is useful for:

  • Displaying the day name in a user interface;
  • Scheduling events or reminders based on weekdays;
  • Applying logic such as highlighting weekends or handling business days.
123456
const date = new Date("2024-06-10"); const weekdayIndex = date.getDay(); const weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; console.log("Day of the week:", weekdays[weekdayIndex]);
copy

Best practices for reading date and time values

  • Always be mindful of zero-based indexes, especially for months;
  • Confirm whether you are working with local time or UTC if precision is required;
  • Use descriptive variable names;
  • Consider mapping numeric values, such as those from getDay(), to readable strings for clarity.

Following these practices helps prevent off-by-one errors and makes your code easier to maintain and understand.

question mark

What value will be printed to the console by the code below, and what does that value represent?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain how to convert the zero-based month to a human-readable format?

How do I get the current date and time as a formatted string?

What is the difference between local time and UTC when using these methods?

Awesome!

Completion rate improved to 7.14

bookReading Date and Time Values

Sveip for å vise menyen

JavaScript provides several methods on the Date object to extract specific components of a date and time. Use the following methods to access these values:

  • getFullYear(): returns the four-digit year;
  • getMonth(): returns the zero-based month index (0 for January, 11 for December);
  • getDate(): returns the day of the month;
  • getHours(): returns the hour of the day (0–23);
  • getMinutes(): returns the minutes (0–59);
  • getSeconds(): returns the seconds (0–59).

These methods allow you to break down a date into its key components for display, calculation, or logic in your application.

123456789
const today = new Date(); const year = today.getFullYear(); const month = today.getMonth(); // Zero-based: January is 0 const day = today.getDate(); console.log("Year:", year); console.log("Month:", month); console.log("Day:", day);
copy
123456789
const now = new Date(); const hours = now.getHours(); const minutes = now.getMinutes(); const seconds = now.getSeconds(); console.log("Hours:", hours); console.log("Minutes:", minutes); console.log("Seconds:", seconds);
copy

The getMonth() method returns a zero-based index for months, meaning January is represented as 0, February as 1, and December as 11. This can lead to confusion if you expect months to start at 1. In the previous code sample, if getMonth() returns 5, it actually refers to June, not May. When displaying or processing month values, add 1 to the result to match common expectations and avoid off-by-one errors.

To read the day of the week from a date, use the getDay() method. This method returns an integer between 0 and 6, where 0 represents Sunday and 6 represents Saturday. Reading the weekday is useful for:

  • Displaying the day name in a user interface;
  • Scheduling events or reminders based on weekdays;
  • Applying logic such as highlighting weekends or handling business days.
123456
const date = new Date("2024-06-10"); const weekdayIndex = date.getDay(); const weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; console.log("Day of the week:", weekdays[weekdayIndex]);
copy

Best practices for reading date and time values

  • Always be mindful of zero-based indexes, especially for months;
  • Confirm whether you are working with local time or UTC if precision is required;
  • Use descriptive variable names;
  • Consider mapping numeric values, such as those from getDay(), to readable strings for clarity.

Following these practices helps prevent off-by-one errors and makes your code easier to maintain and understand.

question mark

What value will be printed to the console by the code below, and what does that value represent?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 2
some-alt