Trying to solve the problem with the time zone in the date object, I succeeded in this parsing function:
function (itemValue, inputFormat) { var value; if (typeof itemValue === "string") { value = inputFormat ? moment(itemValue, inputFormat, true) : moment(itemValue); if (value.isValid && value.isValid()) { value = value.toDate(); } else { console.error("date convert error for", itemValue, inputFormat); } } else if (typeof itemValue === "object" && itemValue.getTimezoneOffset) value = itemValue; else console.error("Date value must be string or date object"); value.setMinutes(0); value.setSeconds(0); value.setMilliseconds(0); value.setHours(-value.getTimezoneOffset() / 60); console.log(value.toLocaleDateString()); return value; }
This function gives a date object with the correct clock as the time zone, so when I request value.toLocaleDateString (), I will get the correct date. I can use this parser to get the date from any server. Moment library is very useful for parsing dates in any format, if you do not provide an input format, the moment can determine which format, but prefers to transmit the input format.
Bogdan Jun 02 '18 at 8:25 2018-06-02 08:25
source share