Javascript date is invalid on iOS
Your date string is not in a format specified to work with new Date
. The only formats in the spec are a simplified version of ISO-8601 (added in ES5 in 2009 and updated in ES2015 and ES2016), and the format output by Date.prototype.toString
. Your string isn't in either format, but it's really close to the ISO-8601-like format. It would also be easy to change it to a format that isn't in the spec, but is universally supported
Four options for you:
- Use the upcoming
Temporal
feature (it's now at Stage 3) - The specified format
- An unspecified format that's near-universally supported
- Parse it yourself
Use the upcoming Temporal
feature
The Temporal
proposal is at Stage 3 as of this update in August 2021. You can use it to parse your string, either treating it as UTC or as local time:
Treating the string as UTC:
// (Getting the polyfill)
const {Temporal} = temporal;
const dateString = "2015-12-31 00:00:00";
const instant = Temporal.Instant.from(dateString.replace(" ", "T") + "Z");
// Either use the Temporal.Instant directly:
console.log(instant.toLocaleString());
// ...or get a Date object:
const dt = new Date(instant.epochMilliseconds);
console.log(dt.toString());
<script src="https://unpkg.com/@js-temporal/polyfill/dist/index.umd.js"></script>
Javascript invalid Date on iphone
I just had an issue like this yesterday, but with internet explorer. I found that using a cross-browser date library like moment.js helped alleviate the issue:
var date = "2013-03-15 10:30:00";
date = moment(date, "YYYY-MM-DD HH:mm:ss").toDate();
Its just a wrapper around the date object, so the toDate() function returns its date object. If you want to take advantage of the formatting options moment provides, just remove the toDate().
Inconsistent behavior of JavaScript Date() function in iOS
You can split the string and then use the Date's constructor:
// Use this constructor
new Date(year, month, day, hours, minutes, seconds, milliseconds)
// code goes like this
let tokens = "2019-03-12 12:30:03".split(/[-: ]/g)
let date = new Date(tokens[0], parseInt(tokens[1]) -1, tokens[2], tokens[3], tokens[4], tokens[5]);
console.log(date);
This solution is working in safari, chrome and firefox browser.
Why is iPhone iOS showing invalid date for momentjs
I had figured out that it's the a
flag in the format string. I changed it to YYYY-MM-DDTHH:mm:ss
and it worked perfectly. Safari must not like am
/pm
data.
Javascript date parsing on Iphone
Not all browsers support the same date formats. The best approach is to split the string on the separator characters (-
,
and :
) instead, and pass each of the resulting array items to the Date
constructor:
var arr = "2010-03-15 10:30:00".split(/[- :]/),
date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]);
console.log(date);
//-> Mon Mar 15 2010 10:30:00 GMT+0000 (GMT Standard Time)
This will work the same in all browsers.
Date on iOS device returns NaN
Fixed this thanks to @Ian his comment ,
changed this:
var actiondate = new Date(date);
to this:
var t = date.split(/[- :]/);
// Apply each element to the Date function
var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
var actiondate = new Date(d);
Related Topics
How to Organize Minification and Packaging of CSS and Js Files to Speed Up Website
Avoid Having to Double-Click to Toggle Bootstrap Dropdown
Why Blocking Event Loop Does Not Block CSS Animation
Can't Change Bootstrap Tooltip Title
How to Over-Ride CSS with User-CSS on Mobile Safari
Webkit Scrollbar Dynamic Styling
JavaScript Style Property Is Empty
How to Change CSS Href="" Using JavaScript
Highlight Multiple Items on Hover's Condition
Is There a Limit on Length of the Key (String) in Js Object
Scrolling Log File (Tail -F) Animation Using JavaScript
How to Include CSS Files from an MVC Partial Control
Saving Dropdown Menu Selection in a Cookie
Random Fullscreen Background Image on Browser Refresh
CSS Class Selector Styles Not Being Applied in React Project
How to Prevent CSS Interference in an Injected Piece of HTML