-0.5 C
New York
Sunday, January 12, 2025

javascript – Why does Day.js return ‘Invalid Date’ for API date strings with timezone offsets in iOS 15 (mPaaS)?


I am engaged on a mission utilizing the Mini Program mPaaS framework by Alibaba Cloud, and I am encountering a problem with date parsing in JavaScript. Particularly, on iOS 15, my date strings (e.g., “2025-01-10 00:00:00.000+0700”) return “Invalid Date”. I think this is because of stricter parsing guidelines within the WebKit engine utilized by the mini-program surroundings. This is my code…

import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";

dayjs.lengthen(utc);
dayjs.lengthen(timezone);

perform dateFormat(datetimeValue) {
  let date = dayjs(datetimeValue);

  if (!date.isValid() && typeof datetimeValue === "string") {
    datetimeValue = datetimeValue.substitute(" ", "T"); // Substitute house with 'T'
    date = dayjs(datetimeValue);
  }

  return date.format("YYYY-MM-DD HH:mm:ss.SSSZ");
}

I am processing date strings from an API, for instance: “2025-01-10 00:00:00.000+0700”. This format works in most environments however returns “Invalid Date” in sure instances, significantly on iOS 15.

My questions are:

  1. Why does Day.js fail to parse this date string in some environments?
  2. Is changing the house with a T ample to repair this challenge?
  3. Ought to I normalize the timezone offset (e.g., +0700 to +07:00)? If sure, how can I implement this successfully?
  4. What’s the easiest way to deal with invalid date strings in Day.js, particularly when working with API knowledge?
    Invalid Date

I’m utilizing Day.js to format date strings returned by an API. The API offers date strings on this format: “2025-01-10 00:00:00.000+0700”. I seen that these strings trigger Day.js to return Invalid Date, significantly on iOS 15
To repair this, I attempted changing the house between the date and time with a

datetimeValue = datetimeValue.substitute(" ", "T");

Nevertheless it nonetheless returned invalid date

I anticipated Day.js to appropriately parse the date strings and permit me to format them into readable codecs like “1 Nov 2023”.
T:anticipated consequence

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles