# Convert A Date Into Timestamp In JavaScript

27th February 2009 - 3 minutes read time

I have previously talked about using PHP to convert from a date to a time, but what about doing the same in JavaScript?

To get the unix timestamp using JavaScript you need to use the getTime() function of the build in Date object. As this returns the number of milliseconds then we must divide the number by 1000 and round it in order to get the timestamp in seconds.

``Math.round(new Date().getTime()/1000);``

To convert a date into a timestamp we need to use the UTC() function of the Date object. This function takes 3 required parameters and 4 optional parameters. The 3 required parameters are the year, month and day, in that order. The optional 4 parameters are the hours, minutes, seconds and milliseconds of the time, in that order.

To create the time do something like this:

``````var datum = new Date(Date.UTC('2009','01','13','23','31','30'));
return datum.getTime()/1000;``````

This prints out 1234567890 as a timestamp. Here is a function to simplify things:

``````function toTimestamp(year,month,day,hour,minute,second){
var datum = new Date(Date.UTC(year,month-1,day,hour,minute,second));
return datum.getTime()/1000;
}``````

Notice that when adding in the month you need to minus the value by 1. This is because the function requires a month value between 0 and 11. However, 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.

``````function toTimestamp(strDate){
var datum = Date.parse(strDate);
return datum/1000;
}``````

This can be run by using the following, not that the date must be month/day when writing the date like this.

``alert(toTimestamp('02/13/2009 23:31:30'));``

Or even this:

``alert(toTimestamp('2009 02 13 23:31:30'));``

Why you're getting `NaN`: When you ask `new Date(...)` to handle an invalid string, it returns a `Date` object which is set to an invalid date (`new Date("29-02-2012").toString()` returns `"Invalid date"`). Calling `getTime()` on a date object in this state returns.

works perfectly thanks

Ravi (Fri, 10/05/2018 - 07:30)

Sweet! Thank you for the information!

Nicephorus Denisov (Fri, 09/27/2019 - 08:53)

Nіce! Thanks a lot for the guide!

Joseph Carroll (Sun, 10/06/2019 - 04:55)

Thanks :)

rüzgar (Fri, 05/08/2020 - 12:57)
{"ft_lang_label":"__label__en","ft_lang_prob":0.7121667,"math_prob":0.8968744,"size":2172,"snap":"2022-27-2022-33","text_gpt3_token_len":522,"char_repetition_ratio":0.12961255,"word_repetition_ratio":0.00591716,"special_character_ratio":0.2753223,"punctuation_ratio":0.16666667,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9607584,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-29T08:01:21Z\",\"WARC-Record-ID\":\"<urn:uuid:aa114746-aa8c-47e9-9143-1c6947970013>\",\"Content-Length\":\"51122\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8209ef21-7b3c-4a56-8e10-c15f32c66688>\",\"WARC-Concurrent-To\":\"<urn:uuid:64c99b91-6bc0-4a10-b012-7f30581a8ce9>\",\"WARC-IP-Address\":\"104.21.26.114\",\"WARC-Target-URI\":\"https://www.hashbangcode.com/article/convert-date-timestamp-javascript\",\"WARC-Payload-Digest\":\"sha1:NAD2N5N7LPRLHCKR5XBMEBH6LBSNNQ5W\",\"WARC-Block-Digest\":\"sha1:6QVFIUFN5ITVQZG462H443CU6YJINY4H\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103624904.34_warc_CC-MAIN-20220629054527-20220629084527-00750.warc.gz\"}"}