199

How can I convert normal date 2012.08.10 to unix timestamp in javascript?

Fiddle: http://jsfiddle.net/J2pWj/




I've seen many posts here that convert it in PHP, Ruby, etc... But I need to do this inside JS.

KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
Stan
  • 25,744
  • 53
  • 164
  • 242

13 Answers13

393
Math.floor(new Date('2012.08.10').getTime() / 1000)

Check the JavaScript Date documentation.

Yangshun Tay
  • 49,270
  • 33
  • 114
  • 141
fguillen
  • 36,125
  • 23
  • 149
  • 210
83
parseInt((new Date('2012.08.10').getTime() / 1000).toFixed(0))

It's important to add the toFixed(0) to remove any decimals when dividing by 1000 to convert from milliseconds to seconds.

The .getTime() function returns the timestamp in milliseconds, but true unix timestamps are always in seconds.

theVinchi
  • 1,185
  • 7
  • 8
  • 8
    just beware that this return a string, not a number. – Capaj Aug 24 '16 at 14:25
  • 4
    Good point, changed answer to wrap with parseInt() to convert back to integer. – theVinchi Oct 20 '16 at 16:29
  • 3
    If performance matters, probably the better and more correct option is `Math.round` or `Math.floor`, rather than converting to a string and then back to an integer - that is: `Math.round(new Date('2012.08.10').getTime() / 1000)` – mindplay.dk Jun 15 '21 at 10:32
  • I haven't tested this out but just putting my two cents in here that I'm thinking this would be very slow compared to the current top method of `Math.floor(new Date('2012.08.10').getTime() / 1000)`. Reason is because this code plays with strings which in my mind would be more performance costly than just dealing with numbers. – jaquinocode Apr 21 '22 at 17:13
40

var d = '2016-01-01T00:00:00.000Z';
console.log(new Date(d).valueOf()); // returns the number of milliseconds since the epoch
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
  • 1
    The only reason this isn't the accepted answer is because it returns the number of *milliseconds* where as the question is about unix timestamps, which are measured in *seconds*. So, you'll still have to do all of the division and Math.floor() that other answers suggest. – Tyler V. May 12 '22 at 22:34
15

You should check out the moment.js api, it is very easy to use and has lots of built in features.

I think for your problem, you could use something like this:

var unixTimestamp = moment('2012.08.10', 'YYYY.MM.DD').unix();
Mahus
  • 595
  • 1
  • 6
  • 16
12

You can do it using Date.parse() Method.

Date.parse($("#yourCustomDate).val())

Date.parse("03.03.2016") output-> 1456959600000

Date.parse("2015-12-12") output-> 1449878400000

Nazar
  • 311
  • 2
  • 6
11
var date = new Date('2012.08.10');
var unixTimeStamp = Math.floor(date.getTime() / 1000);

In this case it's important to return only a whole number (so a simple division won't do), and also to only return actually elapsed seconds (that's why this code uses Math.floor() and not Math.round()).

dumitru
  • 379
  • 2
  • 10
7

You can use Date.parse(), but the input formats that it accepts are implementation-dependent. However, if you can convert the date to ISO format (YYYY-MM-DD), most implementations should understand it.

See Why does Date.parse give incorrect results?.

KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
Barmar
  • 741,623
  • 53
  • 500
  • 612
6

You could simply use the unary + operator

(+new Date('2012.08.10')/1000).toFixed(0);

http://xkr.us/articles/javascript/unary-add/ - look under Dates.

hybrid9
  • 2,466
  • 4
  • 24
  • 24
  • 4
    Just want to add two comments here: the unary add + operator is not needed, as `/` is already a math operator. Also, `toFixed` returns a string and not a number, in case the type is important.. – Laurens Jun 06 '19 at 13:33
1
var datestr = '2012.08.10';
var timestamp = (new Date(datestr.split(".").join("-")).getTime())/1000;
OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
1

After comparing timestamp with the one from PHP, none of the above seems correct for my timezone. The code below gave me same result as PHP which is most important for the project I am doing.

function getTimeStamp(input) {
    var parts = input.trim().split(' ');
    var date = parts[0].split('-');
 var time = (parts[1] ? parts[1] : '00:00:00').split(':');

 // NOTE:: Month: 0 = January - 11 = December.
 var d = new Date(date[0],date[1]-1,date[2],time[0],time[1],time[2]);
 return d.getTime() / 1000;
}

// USAGE::
var start = getTimeStamp('2017-08-10');
var end = getTimeStamp('2017-08-10 23:59:59');

console.log(start + ' - ' + end);

I am using this on NodeJS, and we have timezone 'Australia/Sydney'. So, I had to add this on .env file:

TZ = 'Australia/Sydney'

Above is equivalent to:

process.env.TZ = 'Australia/Sydney'
Damodar Bashyal
  • 931
  • 3
  • 17
  • 31
1

convert timestamp to unix timestamp.

const date = 1513787412; const unixDate = new Date(date * 1000);// Dec 20 2020 (object)

to get the timeStamp after conversion const TimeStamp = new Date(date*1000).getTime(); //1513787412000

Rajat
  • 111
  • 4
  • 14
1

Using dayjs library:

dayjs('2019-01-25').unix() // 1548381600
Sampath
  • 63,341
  • 64
  • 307
  • 441
0

You can use :

Math.round((new Date('specific date')).getTime() / 1000)  //it will return current timestamp in seconds