vee
vee

Reputation: 4765

How to get unix timestamp from Y-m-d date in JS?

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?

Upvotes: 0

Views: 854

Answers (1)

Miloš Rašić
Miloš Rašić

Reputation: 2279

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.

Upvotes: 1

Related Questions