Navigate back to the homepage

How to convert date to timestamp in typescript?

Infinitbility
TypeScript
April 29th, 2022 · 1 min read
keep affilitate

Hi Friends 👋,

Welcome to Infinitbility ❤️!

Today, we are going to learn How to convert a date to the timestamp in typescript?, here we will use the date built-in method Date.parse() method to convert the date to timestamp format.

So, let’s see what is Date.parse() method is?

The Date.parse() method parses a string representation of a date and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).

Here, there is an easier way of getting the timestamp by using the parse() function. This function returns the timestamp in milliseconds, so we need to divide it by 1000 in order to get the timestamp.

let’s dive into code…

1const toTimestamp = (strDate: string) => {
2 var datum: number = Date.parse(strDate);
3 return datum / 1000;
4}
5
6toTimestamp('06/14/2022 11:28:10')

Well, when you run the above code, you can able to see the date convert in timestamp format. For now, let’s check the output.

Output

TypeScript, convert date to timestamp example
TypeScript, convert date to timestamp example

All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

melio affilitate

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to convert date to mm/dd/yyyy in typescript?

TypeScript, convert date to mm/dd/yyyy format example.

April 27th, 2022 · 1 min read

How to convert date to number in typescript?

TypeScript, convert date to number example.

April 27th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]