0

I have a datetime2 coming from sql which displays in a template as follows

{{>~formatDateTime(enddate, "dd/MM/yyyy HH:mm")}}

Which calls the following formatDateTime function

function formatDateTime(dateTime, format)***//the datetime that gets passed in here looks like ("2021-08-01T10:00:40")*** {

    if (dateTime instanceof Date && dateTime.getFullYear() == 2050) {
        return dateTime.format("HH:mm");
    }
    if (!format) {
        format = "dd MMM yyyy";
    }

    if (!(dateTime instanceof Date)) {
        dateTime = new Date(dateTime);
    }

    return dateTime.format(format);
};

in chrome and other browsers it works fine it gives me the datetime as follows 2021/08/01 10:00:40 as it is in sql but in safari it doesnt give me the same datetime back as in sql it returns 2021/08/01 12:00:40

see the time is different

how can i fix this?

josh
  • 29
  • 5
  • That is a known bug in Safari, see [What are valid Date Time Strings in JavaScript?](https://stackoverflow.com/questions/51715259/what-are-valid-date-time-strings-in-javascript). Could you add the timezone to you date strings? Without them, they are ambiguous. – str May 29 '20 at 09:40
  • @str should I add it as "dd/MM/yyyy HH:mm:zz"? – josh May 29 '20 at 09:58
  • 1
    No, you should add the time zone offset as described in the question that I linked earlier. – str May 29 '20 at 10:06
  • Or can I use datetime = new date (datetime).toISOString() – josh May 29 '20 at 10:07
  • 1
    "dd/MM/yyyy HH:mm" is not a format supported by ECMA-262, so the result of `new Date(dateTime)` is implementation dependent. Given that most built-in parsers expect m/d/y, you will likely get unexpected results for most timestamps. – RobG May 29 '20 at 11:57
  • Also note that there are issues with using *instanceof* to test objects, see [Why does AngularJS not use instanceof in its isArray function?](https://stackoverflow.com/questions/25266637/why-does-angularjs-not-use-instanceof-in-its-isarray-function) and [Why date variable is not instanceof Date, even though constructor is Date](https://stackoverflow.com/questions/59117909/why-date-variable-is-not-instanceof-date-even-though-constructor-is-date). – RobG May 29 '20 at 12:02

0 Answers0