Date.parse() in safari - javascript

I am trying to parse a date in safari so that my website will be compatible with IOS as well as everything else, but when I parse a date, it only returns NaN.
The date that I am trying to parse looks like this:
"2015-06-29T23:59:59"
I've looked around for different ways to do this, but I still haven't found a solution to this problem.
Is there anyone who knows a function that will parse the date in safari or a work-around to this problem?
The original approach was: Date.parse("2015-06-29T23:59:59");
Thanks.

The only reliable way to parse a date string is to do it manually, do not rely on Date.parse or passing strings to the Date constructor (which is essentially the same thing).
Before ECMAScript ed 5 (ES5), parsing of strings was entirely implementation dependent. ES5 specified various ISO formats, however there were further changes to parsing with ed 6, so even ISO formats are unreliable and may return local, UTC or NaN dates.
The string "2015-06-29T23:59:59" will be parsed as UTC under ES5, local under ed 6 and before then anything goes (IE8 returns NaN, other browsers from that era may too).
A simple parser, assuming es 6 behaviour and local timezone is:
function parseISO(s) {
var b = s.split(/\D/);
return new Date(b[0], b[1]-1, b[2], b[3], b[4], b[5]);
}
Validating the values and ensuring 2 digit years requires a couple more lines:
function parseISO(s) {
var b = s.split(/\D/);
var d = new Date(b[0], b[1]-1, b[2], b[3], b[4], b[5]);
d.setFullYear(b[0]);
return d && d.getMinutes() == b[4] && d.getMonth() == b[1]-1 && d.getDate() == b[2]? d : new Date(NaN);
}

According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse safari should work with
Date.parse("2015-06-29T23:59:59");
Perhaps post a code snippet that returns NaN, you may be doing something wrong
Having said that, the following may help if safari in IOS is different to the safari referred to in the MDN article
function parseDate(s) {
var dateTime = s.split('T');
var dateBits = dateTime[0].split('-');
var timeBits = dateTime[1].split(':');
return new Date(dateBits[0], parseInt(dateBits[1]) - 1, dateBits[2], timeBits[0], timeBits[1], timeBits[2]).valueOf();
}
parseDate("2015-06-29T23:59:59");
Returns the same value as
Date.parse("2015-06-29T23:59:59");
In firefox, so, that's a start I guess

Related

Uncaught TypeError: time0.getHours is not a function [duplicate]

I have some code that tries to parse a date string.
When I do alert(Date("2010-08-17 12:09:36"));
It properly parses the date and everything works fine but I can't call the methods associated with Date, like getMonth().
When I try:
var temp = new Date("2010-08-17 12:09:36");
alert(temp);
I get an "invalid date" error.
Any ideas on how to parse "2010-08-17 12:09:36" with new Date()?
Date()
With this you call a function called Date(). It doesn't accept any arguments and returns a string representing the current date and time.
new Date()
With this you're creating a new instance of Date.
You can use only the following constructors:
new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
So, use 2010-08-17 12:09:36 as parameter to constructor is not allowed.
See w3schools.
EDIT: new Date(dateString) uses one of these formats:
"October 13, 1975 11:13:00"
"October 13, 1975 11:13"
"October 13, 1975"
The following format works in all browsers:
new Date("2010/08/17 12:09:36");
So, to make a yyyy-mm-dd hh:mm:ss formatted date string fully browser compatible you would have to replace dashes with slashes:
var dateString = "2010-08-17 12:09:36";
new Date(dateString.replace(/-/g, "/"));
The difference is the fact (if I recall from the ECMA documentation) is that Date("xx") does not create (in a sense) a new date object (in fact it is equivalent to calling (new Date("xx").toString()). While new Date("xx") will actually create a new date object.
For More Information:
Look at 15.9.2 of http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf
I know this is old but by far the easier solution is to just use
var temp = new Date("2010-08-17T12:09:36");
Correct ways to use Date : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
Also, the following piece of code shows how, with a single definition of the function "Animal", it can be a) called directly and b) instantiated by treating it as a constructor function
function Animal(){
this.abc = 1;
return 1234;
}
var x = new Animal();
var y = Animal();
console.log(x); //prints object containing property abc set to value 1
console.log(y); // prints 1234
Any ideas on how to parse "2010-08-17 12:09:36" with new Date()?
Until ES5, there was no string format that browsers were required to support, though there are a number that are widely supported. However browser support is unreliable an inconsistent, e.g. some will allow out of bounds values and others wont, some support certain formats and others don't, etc.
ES5 introduced support for some ISO 8601 formats, however the OP is not compliant with ISO 8601 and not all browsers in use support it anyway.
The only reliable way is to use a small parsing function. The following parses the format in the OP and also validates the values.
/* Parse date string in format yyyy-mm-dd hh:mm:ss
** If string contains out of bounds values, an invalid date is returned
**
** #param {string} s - string to parse, e.g. "2010-08-17 12:09:36"
** treated as "local" date and time
** #returns {Date} - Date instance created from parsed string
*/
function parseDateString(s) {
var b = s.split(/\D/);
var d = new Date(b[0], --b[1], b[2], b[3], b[4], b[5]);
return d && d.getMonth() == b[1] && d.getHours() == b[3] &&
d.getMinutes() == b[4]? d : new Date(NaN);
}
document.write(
parseDateString('2010-08-17 12:09:36') + '<br>' + // Valid values
parseDateString('2010-08-45 12:09:36') // Out of bounds date
);
I was having the same issue using an API call which responded in ISO 8601 format.
Working in Chrome this worked:
`
// date variable from an api all in ISO 8601 format yyyy-mm-dd hh:mm:ss
var date = oDate['events']['event'][0]['start_time'];
var eventDate = new Date();
var outputDate = eventDate.toDateString();
`
but this didn't work with firefox.
Above answer helped me format it correctly for firefox:
// date variable from an api all in ISO 8601 format yyyy-mm-dd hh:mm:ss
var date = oDate['events']['event'][0]['start_time'];
var eventDate = new Date(date.replace(/-/g,"/");
var outputDate = eventDate.toDateString();
You can also create single Date object and update it using the static function Date.now() :
<!doctype html>
<html> <head></head>
<body>
<h1 id="datetime"></h1>
<script>
elem_datetime = document.getElementById('datetime');
var date = new Date();
function sometimer()
{
setTimeout(sometimer, 1*1000); // wait 1 second and call again
date.setTime(Date.now());
elem_datetime.innerHTML = date.toDateString() + ' ' + date.toLocaleTimeString();
}
sometimer();
</script>
</body>
</html>
You're not getting an "invalid date" error.
Rather, the value of temp is "Invalid Date".
Is your date string in a valid format?
If you're using Firefox, check Date.parse
In Firefox javascript console:
>>> Date.parse("2010-08-17 12:09:36");
NaN
>>> Date.parse("Aug 9, 1995")
807944400000
I would try a different date string format.
Zebi, are you using Internet Explorer?
I recently ran into this as well and this was a helpful post. I took the above Topera a step further and this works for me in both chrome and firefox:
var temp = new Date( Date("2010-08-17 12:09:36") );
alert(temp);
the internal call to Date() returns a string that new Date() can parse.

Wrong DateTime Conversion in JS [duplicate]

I have some code that tries to parse a date string.
When I do alert(Date("2010-08-17 12:09:36"));
It properly parses the date and everything works fine but I can't call the methods associated with Date, like getMonth().
When I try:
var temp = new Date("2010-08-17 12:09:36");
alert(temp);
I get an "invalid date" error.
Any ideas on how to parse "2010-08-17 12:09:36" with new Date()?
Date()
With this you call a function called Date(). It doesn't accept any arguments and returns a string representing the current date and time.
new Date()
With this you're creating a new instance of Date.
You can use only the following constructors:
new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
So, use 2010-08-17 12:09:36 as parameter to constructor is not allowed.
See w3schools.
EDIT: new Date(dateString) uses one of these formats:
"October 13, 1975 11:13:00"
"October 13, 1975 11:13"
"October 13, 1975"
The following format works in all browsers:
new Date("2010/08/17 12:09:36");
So, to make a yyyy-mm-dd hh:mm:ss formatted date string fully browser compatible you would have to replace dashes with slashes:
var dateString = "2010-08-17 12:09:36";
new Date(dateString.replace(/-/g, "/"));
The difference is the fact (if I recall from the ECMA documentation) is that Date("xx") does not create (in a sense) a new date object (in fact it is equivalent to calling (new Date("xx").toString()). While new Date("xx") will actually create a new date object.
For More Information:
Look at 15.9.2 of http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf
I know this is old but by far the easier solution is to just use
var temp = new Date("2010-08-17T12:09:36");
Correct ways to use Date : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
Also, the following piece of code shows how, with a single definition of the function "Animal", it can be a) called directly and b) instantiated by treating it as a constructor function
function Animal(){
this.abc = 1;
return 1234;
}
var x = new Animal();
var y = Animal();
console.log(x); //prints object containing property abc set to value 1
console.log(y); // prints 1234
Any ideas on how to parse "2010-08-17 12:09:36" with new Date()?
Until ES5, there was no string format that browsers were required to support, though there are a number that are widely supported. However browser support is unreliable an inconsistent, e.g. some will allow out of bounds values and others wont, some support certain formats and others don't, etc.
ES5 introduced support for some ISO 8601 formats, however the OP is not compliant with ISO 8601 and not all browsers in use support it anyway.
The only reliable way is to use a small parsing function. The following parses the format in the OP and also validates the values.
/* Parse date string in format yyyy-mm-dd hh:mm:ss
** If string contains out of bounds values, an invalid date is returned
**
** #param {string} s - string to parse, e.g. "2010-08-17 12:09:36"
** treated as "local" date and time
** #returns {Date} - Date instance created from parsed string
*/
function parseDateString(s) {
var b = s.split(/\D/);
var d = new Date(b[0], --b[1], b[2], b[3], b[4], b[5]);
return d && d.getMonth() == b[1] && d.getHours() == b[3] &&
d.getMinutes() == b[4]? d : new Date(NaN);
}
document.write(
parseDateString('2010-08-17 12:09:36') + '<br>' + // Valid values
parseDateString('2010-08-45 12:09:36') // Out of bounds date
);
I was having the same issue using an API call which responded in ISO 8601 format.
Working in Chrome this worked:
`
// date variable from an api all in ISO 8601 format yyyy-mm-dd hh:mm:ss
var date = oDate['events']['event'][0]['start_time'];
var eventDate = new Date();
var outputDate = eventDate.toDateString();
`
but this didn't work with firefox.
Above answer helped me format it correctly for firefox:
// date variable from an api all in ISO 8601 format yyyy-mm-dd hh:mm:ss
var date = oDate['events']['event'][0]['start_time'];
var eventDate = new Date(date.replace(/-/g,"/");
var outputDate = eventDate.toDateString();
You can also create single Date object and update it using the static function Date.now() :
<!doctype html>
<html> <head></head>
<body>
<h1 id="datetime"></h1>
<script>
elem_datetime = document.getElementById('datetime');
var date = new Date();
function sometimer()
{
setTimeout(sometimer, 1*1000); // wait 1 second and call again
date.setTime(Date.now());
elem_datetime.innerHTML = date.toDateString() + ' ' + date.toLocaleTimeString();
}
sometimer();
</script>
</body>
</html>
You're not getting an "invalid date" error.
Rather, the value of temp is "Invalid Date".
Is your date string in a valid format?
If you're using Firefox, check Date.parse
In Firefox javascript console:
>>> Date.parse("2010-08-17 12:09:36");
NaN
>>> Date.parse("Aug 9, 1995")
807944400000
I would try a different date string format.
Zebi, are you using Internet Explorer?
I recently ran into this as well and this was a helpful post. I took the above Topera a step further and this works for me in both chrome and firefox:
var temp = new Date( Date("2010-08-17 12:09:36") );
alert(temp);
the internal call to Date() returns a string that new Date() can parse.

How to do a date format in Javascript?

I need to understand how to do date formattting in javascript.
i have date as,
var date="12/02/1994";// dd/mm/yyy
var date1=new Date(date);
date1.getDate();// this gives me Month which is 02
date1.getMonth();// this gives me date which is 12.
How do i get the exact date i have in var date in get date and getmonth function? Please help
The answer is pretty simple: JavaScript uses mm/dd/yyyy data format.
It doesn't support dd/mm/yyyy format, so, if you need to parse this format, then you will have to do this manually like this:
function parseDdmmyyyy(str)
{
var spl = str.split('/');
return new Date(spl[2], spl[1] - 1, spl[0]);
}
or you will have to use external libraries like Moment.js.
Javascript date() expects date in mm/dd/yy and not in dd/mm/yy. And months start from 0 and not 1.
var from = "12/02/1994".split("/");
var date1 = new Date(from[2], from[1] - 1, from[0]);
date1.getDate();
date1.getMonth();
Use new Date('02/12/1994'), new Date('1994-02-12') or new Date(1994, 02-1, 12), because in js months start from 0 and american date format is used where month goes first
you can use the simple JS file DateFormat.js which has some very good example through the URL mattkruse (Date Funtion)
from this JS file you can validate the incoming date is a true format even you can add format date within a several ways.
Presumably you want to know how to format strings so they are consistently parsed by browsers. The short answer, is there is no guarantee that any particular string will be correctly parsed by all browsers in use (or perhaps even most).
So the bottom line is: don't parse strings with the Date constructor, ever. It's largely implementation dependent and even the one format specified in ES5 and ECMAScript 2015 is poorly and inconsistently supported.
How browsers treat a string like "12/02/1994" is entirely implementation dependent, however most will treat it as the peculiar US month/day/year format, i.e. 2 December and getMonth will return 11, since months are zero indexed.
So you should always manually parse strings (a library can help, but a simple parsing function is only 2 lines, 3 if validation is required), e.g.
// Parse a date string as d/m/y
// If s is not a valid date, return a Date object with its
// time value set to NaN.
function parseDMY(s) {
var b = s.split(/\D/);
var d = new Date(b[2], --b[1], b[0]);
return d && b[1] == d.getMonth()? d : new Date(NaN);
}
document.write(parseDMY('12/02/1994'));

getDay() errors with Invalid Date in some browsers

I have the following string: "2012-12-10T23:40:41Z"
My goal is to get the day as a number (0-6)... sun-sat
I pass this string as follows:
var input = "2012-12-10T23:40:41Z";
var day = new Date(input).getDay();
alert(day);
This works just fine in Chrome, but in the adobe air webkit view, it errors with, "Invalid Date"
Any suggestions on finding a way to get the day that is supported by older browsers?
Prior to ES5, there was no standard way of parsing a date (each host implemented their own way of parsing). Since ES5, dates must be in the ISO8601 format in order to be parseable in strict mode.
For maximum portability, you should parse the date manually.
More of a comment.
To manually parse a javascript ISO8601 date string, split it into parts and pass it to the date constructor. Note that javascript date strings should always be UTC (Z), so:
function parseISOdateString(s) {
s = s.split(/\D/);
return new Date(Date.UTC(s[0], --s[1], s[2], s[3], s[4], s[5]))
}
You can add bells and whistles to the above to validate the string and the resultant date object if you like.
Note that according to ES5, invalid date strings given to Date.parse (e.g. 2012-12-00) should return NaN, but some browsers will create a date object for 2012-11-30. The above will return a date object for invalid strings, so if consistency with the spec is required:
function parseISOdateString(s) {
s = s.split(/\D/);
var d = new Date(Date.UTC(s[0], --s[1], s[2], s[3], s[4], s[5]))
return (d.getUTCFullYear() == s[0] && d.getUTCDate() == s[2])? d : NaN;
}
That will return NaN for invalid strings like "2012-12-00"
This page should prove helpful, even though it is a bit old. The issue is that ISO 8601 style dates only got support in Javascript 1.8.5.

Difference between Date(dateString) and new Date(dateString)

I have some code that tries to parse a date string.
When I do alert(Date("2010-08-17 12:09:36"));
It properly parses the date and everything works fine but I can't call the methods associated with Date, like getMonth().
When I try:
var temp = new Date("2010-08-17 12:09:36");
alert(temp);
I get an "invalid date" error.
Any ideas on how to parse "2010-08-17 12:09:36" with new Date()?
Date()
With this you call a function called Date(). It doesn't accept any arguments and returns a string representing the current date and time.
new Date()
With this you're creating a new instance of Date.
You can use only the following constructors:
new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
So, use 2010-08-17 12:09:36 as parameter to constructor is not allowed.
See w3schools.
EDIT: new Date(dateString) uses one of these formats:
"October 13, 1975 11:13:00"
"October 13, 1975 11:13"
"October 13, 1975"
The following format works in all browsers:
new Date("2010/08/17 12:09:36");
So, to make a yyyy-mm-dd hh:mm:ss formatted date string fully browser compatible you would have to replace dashes with slashes:
var dateString = "2010-08-17 12:09:36";
new Date(dateString.replace(/-/g, "/"));
The difference is the fact (if I recall from the ECMA documentation) is that Date("xx") does not create (in a sense) a new date object (in fact it is equivalent to calling (new Date("xx").toString()). While new Date("xx") will actually create a new date object.
For More Information:
Look at 15.9.2 of http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf
I know this is old but by far the easier solution is to just use
var temp = new Date("2010-08-17T12:09:36");
Correct ways to use Date : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
Also, the following piece of code shows how, with a single definition of the function "Animal", it can be a) called directly and b) instantiated by treating it as a constructor function
function Animal(){
this.abc = 1;
return 1234;
}
var x = new Animal();
var y = Animal();
console.log(x); //prints object containing property abc set to value 1
console.log(y); // prints 1234
Any ideas on how to parse "2010-08-17 12:09:36" with new Date()?
Until ES5, there was no string format that browsers were required to support, though there are a number that are widely supported. However browser support is unreliable an inconsistent, e.g. some will allow out of bounds values and others wont, some support certain formats and others don't, etc.
ES5 introduced support for some ISO 8601 formats, however the OP is not compliant with ISO 8601 and not all browsers in use support it anyway.
The only reliable way is to use a small parsing function. The following parses the format in the OP and also validates the values.
/* Parse date string in format yyyy-mm-dd hh:mm:ss
** If string contains out of bounds values, an invalid date is returned
**
** #param {string} s - string to parse, e.g. "2010-08-17 12:09:36"
** treated as "local" date and time
** #returns {Date} - Date instance created from parsed string
*/
function parseDateString(s) {
var b = s.split(/\D/);
var d = new Date(b[0], --b[1], b[2], b[3], b[4], b[5]);
return d && d.getMonth() == b[1] && d.getHours() == b[3] &&
d.getMinutes() == b[4]? d : new Date(NaN);
}
document.write(
parseDateString('2010-08-17 12:09:36') + '<br>' + // Valid values
parseDateString('2010-08-45 12:09:36') // Out of bounds date
);
I was having the same issue using an API call which responded in ISO 8601 format.
Working in Chrome this worked:
`
// date variable from an api all in ISO 8601 format yyyy-mm-dd hh:mm:ss
var date = oDate['events']['event'][0]['start_time'];
var eventDate = new Date();
var outputDate = eventDate.toDateString();
`
but this didn't work with firefox.
Above answer helped me format it correctly for firefox:
// date variable from an api all in ISO 8601 format yyyy-mm-dd hh:mm:ss
var date = oDate['events']['event'][0]['start_time'];
var eventDate = new Date(date.replace(/-/g,"/");
var outputDate = eventDate.toDateString();
You can also create single Date object and update it using the static function Date.now() :
<!doctype html>
<html> <head></head>
<body>
<h1 id="datetime"></h1>
<script>
elem_datetime = document.getElementById('datetime');
var date = new Date();
function sometimer()
{
setTimeout(sometimer, 1*1000); // wait 1 second and call again
date.setTime(Date.now());
elem_datetime.innerHTML = date.toDateString() + ' ' + date.toLocaleTimeString();
}
sometimer();
</script>
</body>
</html>
You're not getting an "invalid date" error.
Rather, the value of temp is "Invalid Date".
Is your date string in a valid format?
If you're using Firefox, check Date.parse
In Firefox javascript console:
>>> Date.parse("2010-08-17 12:09:36");
NaN
>>> Date.parse("Aug 9, 1995")
807944400000
I would try a different date string format.
Zebi, are you using Internet Explorer?
I recently ran into this as well and this was a helpful post. I took the above Topera a step further and this works for me in both chrome and firefox:
var temp = new Date( Date("2010-08-17 12:09:36") );
alert(temp);
the internal call to Date() returns a string that new Date() can parse.

Categories