I have a webpage that creates a date from a string. It works fine except for the iphone where I get invalid date.
I have read a small bit about IOS handling dates a little differnt but have not been able to see a fix.
I have opened the page in the stock browser and the latest release of Chrome and get the same error. Works on Android and PC.
dateString = "2013-08-06"
date = new Date(dateString);
I have tried this fix but same error
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]);
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().
Related
According to this answer, Firefox and Chrome accepts the format "YYYY MM DD" while creating a date object.
However, Edge doesn't allow new Date("YYYY MM DD") and wants to be initialized as: new Date("YYYY-MM-DD")
So, should i first check which browser is being used before creating a date object or is there a common pattern by which an date object can be created?
I'm not sure I understant your question because for what I've tried firefox allows you to use new Date("YYYY-MM-DD") and so you could use that and avoid the problem with Edge by using always that, anyway if you're getting an Invalid Date this is my solution by example:
var date;
date = new Date("10 01 01"); //invalid date
if(isNaN(date.getDay())){
date = new Date("2010-01-01")
}
Hope this helps you
As mentioned in the previous answer, new Date("YYYY-MM-DD") should work in Firefox. Test this sample code, for example: http://www.w3schools.com/js/tryit.asp?filename=tryjs_date_string_iso1.
There is a separate issue here related to dates that you may have run into, and that is the aligning of UTC dates with user time zones. This thread has more information about this issue and how to accommodate it: Javascript date object always one day off?
Hope this addresses your concerns!
I've got an odd situation going on here. I've got the following JSON being passed to the time line control:
[
{
"UserId": 2,
"ItemId": 3,
"ItemText": null,
"ItemDate": "2014-06-09T18:51:37",
"ItemDateEnd": null,
"OutcomeScore": null
},
...
]
This is a simple of array of items that I pass to the control to render. In Firefox this renders perfectly, no problems whatsoever. However, Every other browser I've tried it on shows the items +1 hour. I've tried it in Opera, Chrome and IE9 and they are all showing the same problem apart from Firefox. The now time displays as expected on all browsers.
Interestingly I'm in GMT summer time at the moment which is +1h ... but why would this selectively effect browsers differently?
Each browser is running exactly the same query and getting exactly the same JSON. I'm very confused and not even sure where to start looking.
I'm running v2.5.0 of the time line. I've tried updating to the latest version and the same thing occured so I rolled back to 2.5.0 to get the solved before working on getting the latest version integrated into the page.
Anyone seen this and have a solution?
First, note that the Timeline of the CHAP Links library does not support strings as Date, you should provided Dates or a timestamp with a Number (note that the successor of the Timeline, vis.js, does support strings as date). Strings as Date work nowadays because most browsers now support creating dates from an ISO date string.
The issue you have is because you provide an ISO Date String without time zone information. Apparently not all browsers have the same default behavior in that case. Enter the following in a JavaScript console in both Firefox and an other browser:
new Date("2014-06-09T18:51:37").toISOString()
// output is ambiguous, time zone information missing
and you will see them adding timezone information in different ways. To prevent these kind of ambiguities, you should provide timezone information yourself. To specify the time in UTC, add a Z to the end of the string:
new Date("2014-06-09T18:51:37Z").toISOString()
// output is unambiguous
Creating Date objects from strings is unreliable, as you have observed. You should manually parse those strings into Date objects, like this:
// assumes date string is in the format "yyyy-MM-ddTHH:mm:ss"
var dateMatch = dataItem.ItemDate.match(/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/);
var year = parseInt(dateMatch[1], 10);
var month = parseInt(dateMatch[2], 10) - 1; // convert to javascript's 0-indexed months
var day = parseInt(dateMatch[3], 10);
var hours = parseInt(dateMatch[4], 10);
var minutes = parseInt(dateMatch[5], 10);
var seconds = parseInt(dateMatch[6], 10);
var date = new Date(year, month, day, hours, minutes, seconds);
Today I've encountered a problem with dates in JavaScript
I'm trying to display time is format: hour:minute. To do this i've written a test case:
var timeOpts = {hour: "2-digit", minute: "2-digit"};
var dt = new Date('2014-05-08T16:07:51+00:00');
console.log(dt.toLocaleTimeString('uk-UA',timeOpts))
This code works just fine in Chromium (displays 19:07) but in Firefox it does not output time in right format (displays 19:07:51)
If you're concerned about formatting dates correctly across several major browsers, I would go with Moment.js. You can format dates how you want and not worry about browser implementations of toLocaleTimeString(). If you want the format HH:mm, you would use this code:
var dt = new Date('2014-05-08T16:07:51+00:00');
var locale = moment(dt).format("HH:mm");
console.log(locale);
You may think that using Moment.js is a little more than you need, but it really is quite lightweight and it handles all the nitty gritty date manipulations for you and will take into effect browser differences and implementations.
I need to convert Date object to timestamp,so create new Date object from datetime and use getTime,but it makes different result in Chrome and Firefox.it depends on timezone.
var date = new Date('2013-08-26T14:30:00');
date.getTime();
//1377527400000 in Chrome
//1377511200000 in Firefox
date.getTimezoneOffset();
//-270 in both of them
Firefox attention to timezone ,but chrome don't care about it.How can I force Firefox to act like chrome in this situation?And Why they act different?
I'm searching for the way difference than following psudo code:
if (Firefox){
// plus with 270*60*1000
}
--
datetime returned from MySQL,then replace space by T in javascript.
Working Demo Here
try using the standard date/time format:
var date = new Date("mm dd, yy hh:mm:ss");
See your code on JSFiddle
try to use this format:
(new Date('2013-08-26T14:30:00.0Z')).getTime();
and you'l get 1377527400000 for both
Safari browser gives a syntax error when i try to display the date format in
dd/mm/yyyy
format. Is there any solution how to display them using jquery? Thanks in advance
function parseDate(input) {
var d= new Date(input);
return d.format("dd-mm-yyyy");//"dd/mm/yyyy"
}//works on chrome not in safari
I've created a quick demo of your example code using the date format plugin, fiddle here and it appears to work fine in Safari.
Things to try:
Check the format of the date you're passing in to the input is a valid date object
Make sure that the javascript plugin is included in your page correctly, and that an older version is not being cached by Safari.
Failing that, please provide more information about the error Safari gives you.
UPDATE
As you've now said you're passing a string to the Date() object, I can see the problem. Safari is very strict about what it will accept, so you'll need to parse the string first, to form a valid Date object. Try the below:
//var d = new Date("2011-11-02"); // This will work for the vast majority of browsers - but not safari
function parseDate(input) {
var parts = input.match(/(\d+)/g);
return new Date(parts[0], parts[1]-1, parts[2]);
}
var d = parseDate("2011-11-02");
alert(d.format("dd-mm-yyyy"));
You can test it in this fiddle: http://jsfiddle.net/Ly4vb/1/