Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptHow to Convert a Date String to Timestamp in JavaScript ?

How to Convert a Date String to Timestamp in JavaScript ?

In this article, we are going to learn about the conversion of Date strings to Timestamps by using JavaScript. Converting date strings to timestamps means translating human-readable date representations into numeric values, indicating the time’s position in milliseconds since January 1, 1970.

There are several methods that can be used to Convert a Date String to Timestamp in JavaScript, which are listed below:

  • Using Date.parse() Method
  • Using new Date() and getTime() Method
  • Using Date.parse() and Unary Plus Operator
  • Using newDate.valueOf() Method

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Using Date.parse() Method

The date parse() Method is used to know the exact number of milliseconds that have passed since midnight, January 1, 1970, till the date we provide.

Syntax:

Date.parse( datestring );

Example: In this example, we are using the above-explained method.

Javascript




let dateString = "2023-08-23T12:34:56";
let result = Date.parse(dateString);
console.log(result);


Output

1692794096000

Approach 2: Using new Date() and getTime() Methods

In this approach, using new Date() creates a date object from a string, then .getTime() extracts the Unix timestamp (milliseconds since 1970).

Syntax:

Date.getTime()

Example: In this example, the date string “2023-08-23T12:34:56” is first converted into a Date object using new Date(). Then, the .getTime() method is used to obtain the Unix timestamp value

Javascript




let dateString = "2023-08-23T12:34:56";
let date = new Date(dateString);
let result = date.getTime();
console.log(result);


Output

1692794096000

Approach 3 : Using Date.parse() and Unary Plus operator

In this approach, we are using Date.parse() and Unary Plus to Converts date string to Unix timestamp by parsing with Date.parse() and converting result to numerical timestamp using the unary plus operator.

Syntax:

let result = +Date.parse(dateString);

Example: In this example, we are using the above-explained approach.

Javascript




let dateString = "2023-08-23T12:34:56";
let result = +Date.parse(dateString);
console.log(result);


Output

1692794096000

Approach 4: Using newDate.valueOf() Method

In this approach, we are using Date.valueOf() to Converts date string to Unix timestamp by creating a Date object and using its .valueOf() method for numerical timestamp representation.

Syntax:

dateObj.valueOf()

Example: In this example we are using the above-explained apporach.

Javascript




let dateString = "2023-08-23T12:34:56";
let result = new Date(dateString).valueOf();
console.log(result);


Output

1692794096000
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments