0

this might seems to be trivilar, but I havent found a way yet... given this code:

var timestamp = 1579120218;
console.log(timestamp);
console.log(new Date(timestamp * 1000));

1579120218 Thu Jan 16 2020 07:30:18 GMT+1100 (GMT+11:00)

But as Php say, its:

2020-01-15 21:30:18

so I need to have that time without timezone offset (in JS by all means)

John Smith
  • 6,129
  • 12
  • 68
  • 123
  • 2
    Javascript Dates don't have a timezone, they are UTC. *console.log* will typically call *Date.prototype.toString*, which produces a string based on the host's current timezone settings (some environments call *toISOString*). To get UTC values, use either *toISOString* or *toGMTString*. If you specifically want a format of YYYY-MM-DD HH:mm:dd then use a bespoke function or formatting library (see [*How to format a JavaScript date*](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date?r=SearchResults&s=1|1179.9208)). – RobG Jan 21 '20 at 08:46
  • 1
    Are you sending the date in string to PHP ? – Aditya Bhave Jan 21 '20 at 08:52
  • 1
    Following code will give you the UTC Date. const timestamp = 1579120218; const date = new Date(timestamp); const utc = new Date(Date.UTC(date.getYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds())); console.log(date); console.log(utc); If you want the date should be treated as a UTC date, append "Z" to the `Date String` e.g. "1970-01-19T12:08:40.218Z" – Aditya Bhave Jan 21 '20 at 08:55
  • @AdityaBhave, no, I get that timestamp FROM php, and JS has to calculate with it realtime – John Smith Jan 21 '20 at 09:02
  • 1
    OK ... So PHP sends just the timestamp ... then use above code which I shared .. that will help you to get the Date as is. – Aditya Bhave Jan 21 '20 at 09:31

0 Answers0