JavaScript Dates — Interview Questions & Answers
Ques: What is the Date object in JavaScript?
Ans: The Date object in JavaScript is used to work with dates and times — including years, months, days, hours, minutes, seconds, and milliseconds.
Example:
let currentDate = new Date();
console.log(currentDate);
Ques: How do you create a Date object in JavaScript?
Ans: There are 4 main ways to create a Date:
| Syntax | Example | Description |
|---|---|---|
new Date() | new Date() | Current date & time |
new Date(milliseconds) | new Date(0) | Since Jan 1, 1970 |
new Date(dateString) | new Date("2025-10-27") | From string |
new Date(year, month, day, hours, minutes, seconds, ms) | new Date(2025, 9, 27, 14, 0, 0) | Specific date |
Ques: What are different date formats in JavaScript?
Ans: JavaScript automatically converts dates to readable formats:
let d = new Date("2025-10-27");
console.log(d.toString()); // Full string format
console.log(d.toDateString()); // Date only
console.log(d.toISOString()); // ISO standard (used in APIs)
console.log(d.toUTCString()); // UTC format
Ques: How can you get parts of a date in JavaScript?
Ans: Use Get Methods to extract specific date/time components:
| Method | Description | Example |
|---|---|---|
getFullYear() | Year | 2025 |
getMonth() | Month (0–11) | 9 (October) |
getDate() | Day of month (1–31) | 27 |
getDay() | Day of week (0–6) | 1 (Monday) |
getHours() | Hour (0–23) | 14 |
getMinutes() | Minutes | 45 |
getSeconds() | Seconds | 30 |
getMilliseconds() | Milliseconds | 500 |
getTime() | Milliseconds since Jan 1, 1970 | 1730018400000 |
Ques: How can you set parts of a date?
Ans: Use Set Methods to modify date/time values.
setFullYear(year), setMonth(month), setDate(day), setHours(hour), setMinutes(minute), setMilliseconds(ms), setSeconds(second)
Example:
let date = new Date();
date.setFullYear(2030);
date.setMonth(0); // January
console.log(date);
Ques: How do you compare two dates in JavaScript?
Ans: Convert both dates into timestamps using getTime() or direct comparison.
let d1 = new Date("2025-10-27");
let d2 = new Date("2025-12-01");
if (d1 < d2) console.log("d1 is earlier");
Ques: How do you format dates in JavaScript?
Ans: You can use:
toLocaleDateString()→ Localized datetoLocaleTimeString()→ Localized timeIntl.DateTimeFormat→ Custom formatting
Example:
let date = new Date();
console.log(date.toLocaleString("en-IN", { weekday: "long", month: "long", day: "numeric" }));
Ques: How do you get the current timestamp?
Ans: Use any of the following:
Date.now(); // milliseconds since 1970
new Date().getTime(); // same as above
Ques: What is the epoch time in JavaScript?
Ans: Epoch time is January 1, 1970, 00:00:00 UTC. All JavaScript time calculations are based on this reference point.
Ques: How do you find the difference between two dates?
Ans: Subtract one date from another — result is in milliseconds.
let start = new Date("2025-01-01");
let end = new Date("2025-12-31");
let diff = end - start;
let days = diff / (1000 * 60 * 60 * 24);
console.log(days); // 364
Ques: What is the use of Date.UTC()?
Ans: It returns a timestamp (in milliseconds) for a given UTC date and time.
let utc = Date.UTC(2025, 9, 27);
console.log(utc); // milliseconds since epoch
Ques: How do you handle time zones in JavaScript?
Ans: JavaScript dates automatically adapt to the user’s local time zone.
For global consistency, use UTC methods (getUTCDate(), setUTCHours(), etc.) or libraries like Luxon, date-fns, or Moment.js.
Ques: What are best practices when working with dates?
- Always store dates in UTC (ISO 8601) format.
- Use Date objects for time-based calculations.
- Convert to local format only when displaying.
- Avoid manual timezone calculations — use
Intlor date libraries.
