0

Currently, I am developing a website for the company I work for. I am facing one issue at the moment, I have tested my local project on my computer, an android device, and an iPhone.

The problem

I am getting a Date object from the back-end (written in C#). I am trying to parse this to the following string: "HH:MM DD-MM-YYYY".

The method I have written does the job but for some reason, the time on my iPhone is two hours further than the time on my computer or android phone.

Code I am using to parse Date to string

let dateObject;

// InitialCreationDateTime contains the DateTime object that I have received from the back-end
if (this.initialCreationDateTime != null) {
  dateObject = new Date(this.initialCreationDateTime);
} else {
  dateObject = new Date();
}

// InitialCreationDateTime is nothing more then a string variable to store the final date string
this.initialCreationDateTimeString = dateObject.getHours() + ':' + ((dateObject.getMinutes() < 10 ? '0' : '') + dateObject.getMinutes()) + '\xa0\xa0' + dateObject.getDate() + '-' + (dateObject.getMonth() + 1) + '-' + dateObject.getFullYear();

Devices I have used for testing

  • Huawei P20
  • iPhone 5

I hope that my problem is clear, if not, do not hesitate to ask me for more information.

I hope someone can give me some insight/advice.

Thanks in advance

zerk
  • 516
  • 4
  • 9
  • 34
  • This question is similar to: https://stackoverflow.com/questions/17528340/ios-vs-android-different-dates-displayed-for-the-same-program. – Mikail Bayram Jul 24 '19 at 07:51
  • Thanks, @MikailBayram, I will look into that question. – zerk Jul 24 '19 at 07:54
  • I think it's an XY problem. You should ask yourself whether it's the correct approach to use that format specifically, since due to the format itself, some informations are lost (like the timezone). I personally would recommend you to use a completer format instead, like an ISO string, easier to parse in both environments that just **can't** be interpreted differently across environments and devices. – briosheje Jul 24 '19 at 07:54
  • Does your iPhone and Android devices has same time zone? JS Date uses user device date and locale – Justinas Jul 24 '19 at 07:57
  • HH:MM DD-MM-YYYY is not a format supported by ECMA-262, so parsing is implementation dependent. You might get a correct parse, incorrect parse or invalid date, with different results in different hosts. All are compliant. Either write your own parser function (not difficult if the format is known and fixed) or use a library, there are plenty available. – RobG Jul 24 '19 at 09:33

0 Answers0