Mastering Date and Time Manipulation for Efficient Coding

Mastering Date and Time Manipulation for Efficient Coding

Managing dates and times in JavaScript can be a bit tricky, but with the right methods and techniques, it becomes much easier.

Js date object represents a single moment of time on any platform, This moment is defined as 1 Jan 1970, and all the dates declared are calculated from this date.

Usually, dates are calculated in milliseconds, whenever we declare the date object, a long string will come and it is the time in milliseconds from the date (1 Jan 1970).

Getting the Current Date and Time

To get the current date and time, we can use the Date object:

let myDate = new Date();
console.log(myDate); //outputs: 2024-05-21T11:13:58.343Z
// Outputs the full date and time, but not in a human-readable format.

To convert this into a more readable format, we can use the toString() method:

console.log(myDate.toString()); //outputs: Tue May 21 2024 11:42:24 GMT+0000 (Coordinated Universal Time)
// Gives a much better output.

Formatting Dates

JavaScript provides several methods to format dates:

console.log(myDate.toDateString()); //outputs: Tue May 21 2024
// Outputs the date in a readable string format.

console.log(myDate.toLocaleDateString()); //outputs: 5/21/2024
// Outputs the date in the local date format.

console.log(myDate.toJSON()); //outputs: 2024-05-21T11:49:33.033Z
// Outputs the date in JSON format.

console.log(myDate.toISOString()); //outputs: 2024-05-21T11:49:51.018Z
// Outputs the date in ISO string format.

console.log(myDate.toLocaleString()) //outputs: 5/21/2024, 11:50:25 AM
// Outputs the date in locale string format.

Checking the Type of a Date

To verify the type of our date variable, we can use the typeof operator:

console.log(typeof myDate); // Outputs: object

Creating Specific Dates

We can create dates for specific days by passing parameters to the Date constructor. Here are a few examples:

let myCreatedDate = new Date(2024, 0, 23); //Here we have entered January 23, 2024
console.log(myCreatedDate.toDateString()); // Outputs: Tue Jan 23 2024

let anotherCreatedDate = new Date(2024, 0, 23, 5, 3); //Here we have January 23, 2024 at 05:03 AM
console.log(anotherCreatedDate.toLocaleString()); //outputs: 1/23/2024, 5:03:00 AM
// Outputs date and time in local format

let newCreatedDate = new Date("2024-01-14"); // Specific date in YYYY-MM-DD format
console.log(newCreatedDate.toLocaleString()); //outputs: 1/14/2024, 12:00:00 AM
// Outputs date in locale string format

Timestamps in JavaScript

It is a unique identifier that marks the exact moment when an event occurs or a certain action is performed.

example: This is useful when you are developing a quiz or poll and want to know who answered fastest.

Timestamps are useful for comparing dates or measuring time intervals. The Date.now() method returns the current timestamp in milliseconds:

let myCreatedDate = new Date(2024, 0, 23);
let myTimeStamp = Date.now();
console.log(myTimeStamp); //outputs: 1716293998946
// Outputs the current timestamp in milliseconds

console.log(myCreatedDate.getTime()); //outputs: 1705968000000
// Outputs the timestamp of a specific date in milliseconds

If we want the timestamp in seconds instead of milliseconds:

console.log(Math.floor(myTimeStamp / 1000)); // Outputs the timestamp in seconds

Additional Date Methods

JavaScript's Date object comes with various methods to get specific parts of a date:

let newDate = new Date();
console.log(newDate); //outputs: 2024-05-21T12:29:12.452Z
//outputs the date as usually

console.log(newDate.getDate()); //outputs: 21
//Outputs the date of the month

console.log(newDate.getDay()); //outputs: 2
//Outputs the day of the week (0 for Sunday, 6 for Saturday)

console.log(newDate.getMonth()); //outputs: 4
console.log(newDate.getMonth() + 1); //outputs: 5
//Outputs the month (0 for January, 11 for December)

console.log(newDate.getFullYear()); //outputs: 2024
//Outputs the full year

console.log(newDate.getTime()); //outputs: 1716294552452
//Outputs the timestamp in milliseconds

String Interpolation with Dates

We can use string interpolation to embed date values within strings:

let newDate = new Date();
console.log(`Date is ${newDate.getDate()} and the time is ${newDate.getTime()} in milliseconds`);
//outputs: Date is 21 and the time is 1716294905696 in milliseconds

Using toLocaleString for Custom Formats

The toLocaleString method can be customized to format the date according to different locales and options:

newDate.toLocaleString('default', {
    weekday: "long",
});
console.log(newDate); //outputs: 2024-05-21T12:38:51.042Z
// Outputs the day of the week

By utilizing these methods and techniques, you can efficiently manage and manipulate dates in your JavaScript applications. Dates and times are essential aspects of many applications, and mastering their handling will greatly enhance your development skills.