Q1. What is the Date object in JavaScript?
The Date object in JavaScript is used to work with dates and times. It allows developers to create, format, and manipulate date and time values.
JavaScript stores dates internally as the number of milliseconds passed since January 1, 1970 (UTC), which is called the Unix Epoch.
Example
This creates a Date object that contains the current date and time.
JavaScript stores dates internally as the number of milliseconds passed since January 1, 1970 (UTC), which is called the Unix Epoch.
Example
let currentDate = new Date();console.log(currentDate);This creates a Date object that contains the current date and time.
Q2. How can you create a Date object in JavaScript?
There are multiple ways to create a Date object.
1. Current Date and Time
Creates a date object with the current system date and time.
2. Using Specific Date Values
Month starts from 0 (January = 0, February = 1).
3. Using Date String
4. Using Milliseconds
Returns January 1, 1970.
1. Current Date and Time
let date = new Date();console.log(date);Creates a date object with the current system date and time.
2. Using Specific Date Values
let date = new Date(2026, 1, 15);console.log(date);Month starts from 0 (January = 0, February = 1).
3. Using Date String
let date = new Date("2026-02-15");console.log(date);4. Using Milliseconds
let date = new Date(0);console.log(date);Returns January 1, 1970.
Q3. How do you get different parts of a date in JavaScript?
JavaScript provides several built-in methods to extract specific parts of a date.
Example
Example
let date = new Date();
console.log(date.getFullYear());console.log(date.getMonth());console.log(date.getDate());console.log(date.getHours());console.log(date.getMinutes());console.log(date.getSeconds());Q4. How can you set or modify date values?
JavaScript provides setter methods to modify date values.
Example
Example
let date = new Date();
date.setFullYear(2030);date.setMonth(5);date.setDate(10);
console.log(date);Q5. What is the difference between local time and UTC time in JavaScript?
JavaScript supports both local time and Universal Coordinated Time (UTC).
Local time depends on the user’s system timezone, while UTC is a global standard time.
Example
Local time depends on the user’s system timezone, while UTC is a global standard time.
Example
let date = new Date();
console.log(date.getHours()); console.log(date.getUTCHours());- getHours() returns local time.
- getUTCHours() returns UTC time.
