1

I'm trying to achieve this date format..

I'm getting my date from back-end, but its in server time. It looks like this

var d.date = "2017-04-26 07:49:09"

Then, I'm formatting it so date constructor would accept the format properly

var dateFormated = d.date.replace(' ', 'T');

Then, out of it I'm constructing my date object as follows:

var dateplublished = new Date(dateFormated);

Which gives me the following result :

Wed Apr 26 2017 07:49:09 GMT+0300 (Eastern Europe Daylight Time)

Everything seems to be fine, except that the GMT +0300 won't add. It supposed to spit out :

Wed Apr 26 2017 10:49:09 GMT+0300 (Eastern Europe Daylight Time)

How can I achieve this, so it Would be supported in all major browsers including ios safari etc? I managed to do it before, by doing it like so :

var dateFormated = d.date;
var dateplublished = new Date(dateFormated + " " + "UTC");

This way it would spit out everything perfectly, except that it would show NaN-NaN-NaN on ios safari/chrome.

  • Possible duplicate of [How to initialize javascript date to a particular timezone](http://stackoverflow.com/questions/15141762/how-to-initialize-javascript-date-to-a-particular-timezone) – Samuil Petrov Apr 26 '17 at 10:32

2 Answers2

1

You can all Z at the end of date. That way new Date(2017-04-26T07:49:09Z); will be resolved to your local time.

Amit
  • 3,662
  • 2
  • 26
  • 34
0

Date parsing is best with momentjs. It's much more reliable and provides cross-browser support.

Then you can use this to achieve what you're trying to do: https://momentjs.com/docs/#/manipulating/local/

Max Prais
  • 66
  • 6