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

bookCreating Dates

When working with dates and times in JavaScript, you will use the built-in Date object. The Date object allows you to create, manipulate, and display dates and times in your applications. Whether you need to show the current time, schedule future events, or work with timestamps, understanding how to create date objects is fundamental.

123
// Creating a Date object for the current date and time const now = new Date(); console.log(now); // Outputs the current date and time, e.g., 2024-06-10T14:23:30.123Z
copy
1234
// Creating a Date object for a specific date and time // Note: Months are zero-indexed (0 = January, 11 = December) const birthday = new Date(1995, 4, 15, 8, 30, 0); // May 15, 1995, 08:30:00 console.log(birthday); // Outputs: 1995-05-15T08:30:00.000Z (in UTC)
copy
1234
// Creating a Date object from a timestamp (milliseconds since Jan 1, 1970) const timestamp = 1609459200000; // Represents 2021-01-01T00:00:00.000Z const dateFromTimestamp = new Date(timestamp); console.log(dateFromTimestamp); // Outputs: 2021-01-01T00:00:00.000Z
copy

There are two common ways to get the current moment in JavaScript: using new Date() and using Date.now(). The new Date() constructor returns a new Date object representing the current date and time, which you can use to access or manipulate individual components such as year, month, or day. In contrast, Date.now() returns the number of milliseconds that have elapsed since January 1, 1970 00:00:00 UTC (the Unix epoch). Use new Date() when you need a full date object for working with date parts or formatting. Use Date.now() when you only need the current timestamp for calculations or comparisons.

123
// Getting the current timestamp in milliseconds const currentTimestamp = Date.now(); console.log(currentTimestamp); // Outputs a number like 1718035410123
copy

Each method of creating dates serves a different purpose:

  • Use new Date() with no arguments when you want the current date and time as a Date object, such as for logging events or displaying the current time to users;
  • When you need to create a specific date—like a user's birthday or a scheduled meeting—use new Date(year, month, day, hours, minutes, seconds), remembering that months are zero-based;
  • To work with data stored as timestamps (milliseconds since the Unix epoch), use new Date(milliseconds) to convert the timestamp into a readable date object;
  • If you only need the precise moment in milliseconds for timing operations or performance measurement, use Date.now() for efficiency.

Understanding these options will help you choose the right approach for any date and time scenario in your JavaScript projects.

question mark

Which method should you use to create a Date object representing the current date and time in JavaScript?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 1

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

Awesome!

Completion rate improved to 7.14

bookCreating Dates

Sveip for å vise menyen

When working with dates and times in JavaScript, you will use the built-in Date object. The Date object allows you to create, manipulate, and display dates and times in your applications. Whether you need to show the current time, schedule future events, or work with timestamps, understanding how to create date objects is fundamental.

123
// Creating a Date object for the current date and time const now = new Date(); console.log(now); // Outputs the current date and time, e.g., 2024-06-10T14:23:30.123Z
copy
1234
// Creating a Date object for a specific date and time // Note: Months are zero-indexed (0 = January, 11 = December) const birthday = new Date(1995, 4, 15, 8, 30, 0); // May 15, 1995, 08:30:00 console.log(birthday); // Outputs: 1995-05-15T08:30:00.000Z (in UTC)
copy
1234
// Creating a Date object from a timestamp (milliseconds since Jan 1, 1970) const timestamp = 1609459200000; // Represents 2021-01-01T00:00:00.000Z const dateFromTimestamp = new Date(timestamp); console.log(dateFromTimestamp); // Outputs: 2021-01-01T00:00:00.000Z
copy

There are two common ways to get the current moment in JavaScript: using new Date() and using Date.now(). The new Date() constructor returns a new Date object representing the current date and time, which you can use to access or manipulate individual components such as year, month, or day. In contrast, Date.now() returns the number of milliseconds that have elapsed since January 1, 1970 00:00:00 UTC (the Unix epoch). Use new Date() when you need a full date object for working with date parts or formatting. Use Date.now() when you only need the current timestamp for calculations or comparisons.

123
// Getting the current timestamp in milliseconds const currentTimestamp = Date.now(); console.log(currentTimestamp); // Outputs a number like 1718035410123
copy

Each method of creating dates serves a different purpose:

  • Use new Date() with no arguments when you want the current date and time as a Date object, such as for logging events or displaying the current time to users;
  • When you need to create a specific date—like a user's birthday or a scheduled meeting—use new Date(year, month, day, hours, minutes, seconds), remembering that months are zero-based;
  • To work with data stored as timestamps (milliseconds since the Unix epoch), use new Date(milliseconds) to convert the timestamp into a readable date object;
  • If you only need the precise moment in milliseconds for timing operations or performance measurement, use Date.now() for efficiency.

Understanding these options will help you choose the right approach for any date and time scenario in your JavaScript projects.

question mark

Which method should you use to create a Date object representing the current date and time in JavaScript?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 1
some-alt