0

I have this date. 2014-11-15
The timestamp from PHP is 1415984400

How to get this timestamp in Javascript?

These are what i tried and the result.

Date.parse('2014-11-15').getTime()/1000

Error: Date.parse(...).getTime is not a function

Date.parse('2014-11-15')/1000

1416009600

new Date('2014-11-15').getTime() / 1000;

1416009600

None of them convert correctly.

Update: -----

<?php echo date('Y-m-d H:i:s', '1416009600'); ?> 

Result:

2014-11-15 07:00:00

It looks like GMT problem. (Here is GMT+7) How to convert to unix timestamp in Javascript language?

vee
  • 4,506
  • 5
  • 44
  • 81

1 Answers1

1

JavaScript is returning the correct result for GMT, but you are expecting another time zone. You have to fix the result yourself. If you are ok with trusting whatever settings JavaScript is using the determine the local time, you can use:

(New Date()).getTimezoneOffset()

This method returns minutes so you need to multiply the returned value by 60 before subtracting it from the unix timestamp.

Miloš Rašić
  • 2,229
  • 5
  • 24
  • 43
  • (new Date(date_str).getTime() / 1000) + ((new Date()).getTimezoneOffset()*60) Thank you. :) – vee Mar 02 '15 at 12:15
  • This isn't really an answer. The format in the OP will be treated as invalid by some browsers (such as IE 8), local by some (such as some versions of Chrome, Safari and maybe others) and UTC by the rest. There are many other ECMAScript implementations that may do any one of the above. – RobG Feb 26 '16 at 02:37