-1

When getting the time in Chrome and Firefox I get the number of milliseconds for a given date, but for Safari it Shows NaN. Any ideas why this would happen? I would need it to show also the number of milliseconds -as well- in Safari.

PHP code

$date = $product->getDateEnd(); 

Javascript:

var countDownDate = new Date("<?php echo $date ?>").getTime();
Eduardo
  • 53
  • 8

1 Answers1

1

I guess you got tis pattern yyyy-mm-dd. This isn't an officially supported Date pattern, some browsers like Chrome and Firefox will support it, but Safari not. Try to use other versions, like :

yyyy/mm/dd mm/dd/yyyy mm-dd-yyyy

oma
  • 1,804
  • 12
  • 13
  • Then why is it that an alert on this variable: var countDownDate = new Date("").getTime(); would return NaN in Safari but an alert on this variable var now = new Date().getTime(); Would return the correct number of miliseconds in safari? – Eduardo Feb 06 '19 at 09:34
  • because your `new Date("")` is returning an `invalid date` To compare, try this in chrome or firefox: `new Date("badDateFormat").getTime();` Will return `NaN` – oma Feb 06 '19 at 09:42
  • Thanks for clarifying. Is there something I can add to new Date("").getTime() in order to make it return a valid date? – Eduardo Feb 06 '19 at 09:57
  • format the `"` to a valid date for safari. But why you need the date from PHP ?? – oma Feb 06 '19 at 10:02
  • Because that's a date that has been configured at the Magento backend for each product, so the dates can vary per product. – Eduardo Feb 06 '19 at 10:11
  • then just save it to a variable and format it properly for safari. – oma Feb 06 '19 at 11:17
  • I've formatted it to yyyy/mm/dd and then it's working as intended; thank you – Eduardo Feb 06 '19 at 15:15