Date and Time
Data Types: Date and Time
Can you define what the Date object is in JavaScript?
View Answer:
Creating a Date
object representing the current date and time:
const currentDate = new Date();
console.log(currentDate);
// logs Wed Jun 21 2023 16:45:26 GMT-0700 (Pacific Daylight Time)
Creating a Date
object with a specific date and time:
const specificDate = new Date('2023-06-21T10:30:00');
console.log(specificDate);
Getting various components of a Date
object:
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth(); // Month is zero-based (0-11)
const day = date.getDate();
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
console.log(year, month, day, hours, minutes, seconds);
Performing calculations with Date
objects:
const now = new Date();
const futureDate = new Date(now.getFullYear() + 1, now.getMonth(), now.getDate());
console.log(futureDate);
In this example, we calculate a future date by adding one year to the current date using the getFullYear
, getMonth
, and getDate
methods of the Date
object.
The Date
object provides many more methods and properties to work with dates and times, allowing you to perform various operations and manipulate dates according to your needs.
What is the purpose of the Date object in JavaScript?
View Answer:
let date = new Date();
console.log(date); // Logs the current date and time
How can you get the current date and time in JavaScript?
View Answer:
let date = new Date();
console.log(date); // Logs the current date and time
Does JavaScript have a time object?
View Answer:
Getting the current time:
const currentTime = new Date();
console.log(currentTime.toLocaleTimeString());
Setting a specific time:
const specificTime = new Date();
specificTime.setHours(10);
specificTime.setMinutes(30);
specificTime.setSeconds(0);
console.log(specificTime.toLocaleTimeString()); // logs 10:30:00 AM
How do you create a new Date object in JavaScript?
View Answer:
let currentDate = new Date();
console.log(currentDate); // Logs the current date and time
What is an integer number representing the number of milliseconds that have passed since the beginning of 1970 called?
View Answer:
// 31 Dec 1969
let Dec31_1969 = new Date(-24 * 3600 * 1000);
console.log(Dec31_1969);
Can you use a date string format to create dates in JavaScript?
View Answer:
Syntax: new Date(“datestring”);
let date = new Date('2017-01-26');
console.log(date);
// The time is not set, so it's assumed to be midnight GMT and
// is adjusted according to the timezone the code is run in
// So the result could be
// Thu Jan 26 2017 11:00:00 GMT+1100 (Australian Eastern Daylight Time)
// or
// Wed Jan 25 2017 16:00:00 GMT-0800 (Pacific Standard Time)
What is the difference between Date.parse() and Date(datestring)?
View Answer:
let date = Date.parse('01-02-2021'); // get timestamp returns 1609574400000
let date2 = new Date(1609574400000); // using timestamp
console.log(date); // returns 1609574400000
console.log(date2); // returns 2021-01-02T08:00:00.000Z
What is the difference between the getYear() and getFullYear() methods?
View Answer:
let date = new Date();
let year = date.getFullYear();
console.log(year); // returns 2021
The getYear() method is deprecated, and you should not use it. Use getFullYear() instead.
Can you explain the function of the date object getMonth() method?
View Answer:
Syntax: dateObj.getMonth();
let Xmas95 = new Date('December 25, 1995 23:15:30');
let month = Xmas95.getMonth();
console.log(month); // 11
Can you please explain the function of the date object getDate() method?
View Answer:
Syntax: dateObj.getDate();
let Xmas95 = new Date('December 25, 1995 23:15:30');
let day = Xmas95.getDate();
console.log(day); // 25
How does the object getDay() method function?
View Answer:
Syntax: dateObj.getDay();
let Xmas95 = new Date('December 25, 1995 23:15:30');
let weekday = Xmas95.getDay();
console.log(weekday); // 1
What does UTC stand for in programming?
View Answer:
What is UTC, regarding date time, and are there any methods to process UTC in JavaScript?
View Answer:
const date = new Date();
const utcYear = date.getUTCFullYear();
const utcMonth = date.getUTCMonth(); // Month is zero-based (0-11)
const utcDate = date.getUTCDate();
const utcHours = date.getUTCHours();
const utcMinutes = date.getUTCMinutes();
const utcSeconds = date.getUTCSeconds();
console.log(utcYear, utcMonth, utcDate, utcHours, utcMinutes, utcSeconds);
Is there a way to set dates and times in JavaScript?
View Answer:
let today = new Date();
today.setHours(0);
console.log(today); // still today, but the hour is changed to 0
today.setHours(0, 0, 0, 0);
console.log(today); // still today, now 00:00:00 sharp.
Data commonly has out-of-range date information. How does JavaScript handle dates that are formatted incorrectly?
View Answer:
let date = new Date(2013, 0, 32); // 32 Jan 2013 ?!?
console.log(date); // ...is 1st Feb 2013!
// Another example
let date = new Date(2016, 1, 28);
date.setDate(date.getDate() + 2);
console.log(date); // 1 Mar 2016
What happens when a Date object converts to a number?
View Answer:
// Date to number conversion using the unary operator
let date = new Date();
console.log(+date); // returns the number of milliseconds, same as date.getTime()
// Time Measurement: Speed Test
let start = new Date(); // start measuring time
// do the job
for (let i = 0; i < 100000; i++) {
let doSomething = i * i * i;
}
let end = new Date(); // end measuring time
console.log(`The loop took ${end - start} ms`);
The critical side effect: JavaScript can subtract date numbers; the result is their difference in milliseconds. That can be used for time measurements and equip an application with more powerful time-related features.
How does the Date.now() method function in JavaScript?
View Answer:
Syntax: Date.now();
let start = Date.now(); // milliseconds count from 1 Jan 1970
// do the job
for (let i = 0; i < 100000; i++) {
let doSomething = i * i * i;
}
let end = Date.now(); // done
console.log(`The loop took ${end - start} ms`); // subtract numbers, not dates
Can you explain the function of the Date.parse() method?
View Answer:
Syntax: Date.parse();
let ms = Date.parse('2012-01-26T13:51:50.417-07:00');
console.log(ms); // 1327611110417 (timestamp)
// We can instantly create a new Date object from the timestamp
let date = new Date(Date.parse('2012-01-26T13:51:50.417-07:00'));
console.log(date);
What is the Unix Epoch time, and how can you convert a Unix timestamp to a JavaScript Date object?
View Answer:
const unixTimestamp = 1624298400; // Unix timestamp representing July 21, 2021, 00:00:00 UTC
const date = new Date(unixTimestamp * 1000);
console.log(date);
What is the difference between UTC and local time in JavaScript?
View Answer:
How can you format a date to specific in JavaScript?
View Answer:
How can you calculate the difference between two dates in JavaScript?
View Answer:
let date1 = new Date('2023-05-23');
let date2 = new Date('2023-05-30');
// Get the difference in milliseconds and convert it to days
let diffDays = Math.abs(date2 - date1) / (1000 * 60 * 60 * 24);
console.log(diffDays); // Outputs 7
How can you add or subtract time from a Date object in JavaScript?
View Answer:
let date = new Date();
date.setTime(date.getTime() + (3*60*60*1000)); // adds 3 hours
let date = new Date();
date.setTime(date.getTime() - (30*60*1000)); // subtracts 30 minutes
Can we use the getTime() method to add or subtract from the Date Object?
View Answer:
let date = new Date();
date.setTime(date.getTime() + (3*60*60*1000)); // adds 3 hours
What is the maximum and minimum date that can be represented in JavaScript?
View Answer:
In days, JavaScript can represent dates from -100,000,000 days to 100,000,000 days relative to January 1, 1970 UTC, about 273,790 BCE to 273,790 CE.