0

since many days i'm facing a problem to convert the server UTC time zone to client's local time zone when displaying time in my ionic mobile app. I tried the following ways {{consts.convertToUTCDate(Message.mtmdmessages.sentDateTime) | date:'yyyy-MM-dd hh:mm Z'}}

{{consts.convertToUTCDate(Message.mtmdmessages.createDateTime) | date : 'short' : timezone}}

{{consts.convertToUTCDate(Message.mtmdmessages.createDateTime) | date:'d MMMM yyyy HH:mm:ss' : 'UTC'}}

where in consts.ts file i have the method as follows

convertToUTCDate(date) {
     var dd =  new Date(new Date(date).toUTCString().substr(0, 25))
       console.log("the date in ist is" + new Date(date));
       console.log("date in utc is " + dd);
       return dd;
      }

here parameter date is a number (for eg: 1509621234939 )

even i tried in this way :

var now = new Date(1509608135985);

var isoDate = new Date(now.getTime() - now.getTimezoneOffset() * 60000).toISOString();

console.log(isoDate);
isoDate = new Date(now).toISOString();
console.log(isoDate);

console.log(new Date(new Date().toUTCString().substr(0, 25)));

when i executed the above code in browser console it displays correct UTC timezone to IST , but when it is displayed in ionic App it is showing wrong time . can you suggest me what to do?

  • 1
    Date objects don't have a timezone, they are always UTC. Host settings are used to determine the local timezone offset and (less reliably) the timezone when parsing to get the UTC equivalent. Host settings are also used when formatting output for *toString* and *toLocaleString*. Also see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results). – RobG Nov 02 '17 at 22:54
  • Also, the value returned by [*toUTCString*](http://ecma-international.org/ecma-262/8.0/#sec-date.prototype.toutcstring) is implementation dependent, so the result of `new Date(new Date().toUTCString().substr(0, 25))` is unreliable. I suggest that you work only in UTC and generate local values for display purposes only. – RobG Nov 03 '17 at 00:29
  • @RobG Thanks for your reply , can you share your idea how to do it in javascript . – Reyansh Krishna Nov 03 '17 at 05:18

0 Answers0