Safari Can’t Parse ‘2018‑12‑10 11:11:11’: Why and How to Fix It
This article explains why Safari on iOS throws “Invalid Date” for date strings like “2018‑12‑10 11:11:11”, demonstrates the incompatibility with Chrome, and provides a simple regex‑based solution to convert the format so the date parses correctly across browsers.
When converting a string like "2019.06.06 13:12:49" to a timestamp with new Date(), Chrome works but Safari on iOS throws “Invalid Date”. The problem is Safari only parses dates in the yyyy/mm/dd format.
Testing with both “2018.12.10 11:11:11” and “2018-12-10 11:11:11” confirms that Safari fails on the dot or hyphen variants.
The fix is to replace the dot or hyphen with a slash before creating the Date object:
let dateStr1 = '2018.12.10 11:11:11';
let dateStr2 = '2018-12-10 11:11:11';
function dateToTimestamp(dateStr) {
if (!dateStr) {
return '';
}
let newDate = dateStr.replace(/\.|\-/g, '/');
let date = new Date(newDate);
let timestamp = date.getTime();
return timestamp;
}
dateToTimestamp(dateStr1);
dateToTimestamp(dateStr2);According to a Stack Overflow discussion, Safari’s new Date(string) only supports the yyyy/mm/dd format, while Chrome accepts both.
Tip: When developing mobile web or mini‑programs, be aware of such browser‑specific date parsing quirks to avoid compatibility pitfalls.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaoDou Frontend Team
Open-source, innovative, collaborative, win‑win – sharing frontend tech and shaping its future.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
