1

I am trying to compare two dates which works properly on every device except the iPhone.Its giving wrong alert on iPhone.

jsfiddle

function play_time() {
    var crntTime;
    var today = new Date();
    var formattedTime = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
    var dd = today.getDate();
    var mm = today.getMonth() + 1;
    var yyyy = today.getFullYear();
    if (dd < 10) {
        dd = '0' + dd;
    }
    if (mm < 10) {
        mm = '0' + mm;
    }
    today = yyyy + '-' + mm + '-' + dd;
    var crntDatetime = `${today} ${formattedTime}`;
    crntTime = new Date(crntDatetime).getTime();

    var check = '2020-08-18 23:14:07';

    var gameTime = new Date(check).getTime();

    if (crntTime <= gameTime) {
        alert('Play');
    } else {
        alert('Later');
    }
}
<button onClick="play_time();"> show Time </button>
lipon
  • 11
  • 1
  • 14
  • The Date constructor varies from platform to platform. You're passing in a format that the iOS code does not recognize. – Pointy Aug 19 '20 at 15:09
  • @Pointy Which format should i use? – lipon Aug 19 '20 at 15:10
  • See Mr Crowder's answer. Anything other than the ISO standard is risky. Another alternative is to use a library like Moment that allows you to explicitly tell it what your date format means. – Pointy Aug 19 '20 at 15:15
  • Also your formatted time is not actually formatted. You need to pad that too in my opinion. Much better to use a proper date: `const now = new Date().getTime(); const check = new Date(yyyy,mm-1,dd,hh,min,ss,0).getTime(); if (now <= check) ...` – mplungjan Aug 19 '20 at 15:18
  • @mplungjan Its still returning NAN for this `var check = '2020-08-18 23:14:07';` `var gameTime = new Date(check).getTime();` – lipon Aug 19 '20 at 18:17
  • So create the date the way I suggest – mplungjan Aug 19 '20 at 18:42
  • @mplungjan How can i do that? – lipon Aug 19 '20 at 19:33
  • @mplungjan It is giving wrong result `-1321942553000` with that date `gameTime`. – lipon Aug 19 '20 at 20:16
  • Sorry. I was on my phone `const [d,t] = check.split(" "), [yyyy,mm,dd]=d.split("-"), [hh,min,ss]=t.split(":"), gameTime=new Date(yyyy,mm-1,dd,hh,min,ss,0).getTime();` – mplungjan Aug 19 '20 at 20:21
  • And choose a date in the near future too – mplungjan Aug 19 '20 at 20:21
  • 1
    @mplungjan Its working now, Thank you. – lipon Aug 19 '20 at 20:34

1 Answers1

3

By using a space between the date and time in your check example, you're straying outside of the specification. That means that the implementation can use local time, or UTC, or refuse to parse it entirely.

Use the date time format in the specification which is basically what you have but with a T instead of space, or use the multi-argument Date constructor (for local time), or Date.UTC (passing the result into the Date constructor) for UTC. If parsing a string ideally always specify Z for UTC or a timezone offset. Date-only forms ("2020-08-19") are parsed in UTC but date-time forms ("2020-08-19T00:00:00") are parsed in local time — but, that changed more than once during the years 2015-2017 so it's a bit risky to rely on it.

In your situation, since you already have the information as separate variables, I would definitely use the multi-argument version of new Date or Date.UTC+new Date (depending on whether you want local or UTC).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875