Calculate TimeStamp

Walter Nascimento
Walter Nascimento | EN
2 min readSep 2, 2020

Simple timestamp calculator from data.

[clique aqui para português]

When we are working with dates we sometimes encounter dates in milliseconds (timestamp), in some cases it is good to work with and in other cases it is complicated, but to make it easier we will create a date convert to timestamp and the other way around: timestamp to date.

TimeStamp (description)

A timestamp represents a single moment, its value corresponds to a certain amount of time elapsed from an initial moment.

This instant is called UnixEpoch, its value is 01/01/1970 00:00:00 UTC, for the Brazilian time zone the start date is 31/12/1969 21:00:00.

CODE

First we will create the interface, we will do something simple, using only HTML.

HTML code

In the HTML structure, two divisions were made using the fieldset, one to convert from data to timestamp and the other to do the reverse.

Now let’s create the dateToTimestamp function.

JS code — Function dateToTimestamp

In this function (dateToTimestamp), the value of the starting date and time is retrieved, then the function getTime () is used, which retrieves the value of the date directly in timestamp.

Now let’s create the timestampToDate function.

JS code — Function timestampToDate

In this function (timestampToDate), the value of the timestamp is retrieved and the reverse process is performed, transforming it back into a date and time.

ready as simple as that.

Demo

See the complete project working below.

Project on codepen

Youtube

If you prefer to watch, I see the development on youtube (video in PT-BR).

Video on youtube

Thanks for reading!

If you have any questions, complaints or tips, you can leave them here in the comments. I will be happy to answer!

😊😊See you! 😊😊

--

--