0

I'm using javascript new Date format. It is working well in android and windows but when I use it on iphone it outputs different result. How can I be able to solve the problem?

   var rawGetDate = "2019-02-01 00:00:00";
   var dateF = new Date(rawGetDate.replace(' ', 'T'));
   console.log(dateF);

OutPut in Androaid: Tue Jan 01 2019 00:00:00 GMT+0600 
OutPut in Iphone: Mon Dec 01 2018 00:00:00 GMT+0500 
Vebbie
  • 1,669
  • 2
  • 12
  • 18
Raw Scripter
  • 113
  • 1
  • 12
  • I doubt the output is like that on iPhone: 1 December 2018 was not a Monday. Did you mean 31 December? – trincot Feb 04 '19 at 19:26
  • Don't rely on `Date#toString()` Format it specifying a timezone. You can try the following for limited mileage. `console.log(new Date().toLocaleTimeString('en-US', {timeZone: 'America/Denver'}))` See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString#Parameters or https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat – Ruan Mendes Feb 04 '19 at 19:29
  • Make sure to add the timezone indication to the string that you pass to `new Date`: for instance, for UTC, just add a "Z" at the end. – trincot Feb 04 '19 at 19:29
  • @trincot That will not print the date in the specified time zone, it will only read it in then print it in the local time zone – Ruan Mendes Feb 04 '19 at 19:30
  • @JuanMendes, I know, but the output shows that, when looking at the printed timezone informations, they are not even the same date/time in the respective Date objects, otherwise they would have only differed with 1 hour. So also the loading part needs correction. There is clearly a difference in interpretation of the string when specified without timezone. – trincot Feb 04 '19 at 19:35
  • @trincot sorry my bad. you are right . its 31 December – Raw Scripter Feb 04 '19 at 20:47
  • And your original string is really 1 February? Or is it 1 January (2019-01-01)? – trincot Feb 04 '19 at 20:50
  • @trincot 2019-01-01 – Raw Scripter Feb 04 '19 at 20:55
  • 2
    It is a pity that your question had two mistakes (so far). You really should pay more attention when asking a question ;-) One more: are the time parts really "00:00:00" on *both* devices, or does one have "23:00:00" or "01:00:00"? – trincot Feb 04 '19 at 20:58
  • 1
    Note that Safari incorrectly parses "2019-02-01T00:00:00" as UTC instead of local. Please **do not** use the built–in parser, a simple parser for an ISO–like format is 2 lines of code. The built–in parser has been unreliable for 20 years, that won't change anytime soon. – RobG Feb 04 '19 at 22:08

0 Answers0