How to fix parsing error by moment.js?
Const.DATE_MONTH_FORMAT = 'DD-MMM-YYYY';
var fromDate = moment(new Date(period.fromDate)).format(Const.DATE_MONTH_FORMAT);
On Firefox, Chrome: 19-Aug-2016
On Safari: Invalid date
On Firefox:
console.debug(period);
Object { fromDate="19/Aug/2016", toDate="30/Aug/2016"}
format() is used for output, not for determining what format the input is in. You're also passing period.fromDate to a Date constructor, rather than to moment, so you're using the browser's own parsing, which can do whatever it likes when dates don't conform to the JS spec (which DD/MMM/YYYY doesn't). Firefox and Chrome evidently think they can pluck a date from period.fromDate, while Safari can't. You should be able to fix this by passing in the string directly to moment, along with the format of period.fromDate, so it can do all the work itself:
Const.DATE_MONTH_FORMAT = 'DD-MMM-YYYY';
var fromDate = moment(period.fromDate, "DD/MMM/YYYY").format(Const.DATE_MONTH_FORMAT);
Related
I ma using code like
$filter('date')(new Date(), $scope.format);
$scope.format = 'dd-MMM-yyyy';
It works properly in all browsers except firefox. Firefox show value as NaN.
How do I fix this issue?
if your code involves new Date('<dd-MMM-yyyy>') at some point,
I would like to point it out that Firefox doesn't take that format!
new Date('<dd/MMM/yyyy>') would be fine.
This may happen due differing implementations in browsers when parsing date strings. The browser will use the locale to parse the date string, and as locals are subject to change, the returning string may not match the parsing method.
You can avoid it by returning the date's time in milliseconds instead of the date's string (which happens when calling the date's empty constructor).
Replace new Date() with (new Date()).getTime(), and you should be fine.
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
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().
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/
The page works fine in Chrome, but I have this one minor error in Firefox and a different problem in IE. Assistance with either of these issues is greatly appreciated. Since I've been stumped in the Firefox error the longest, I'll start with that one:
Here's the code: http://truxmapper.appspot.com/sched.html
The date picker selects a date using the format "07-08-2010 23:28". Now, I need to pass this time as a parameter to my servlet, which is expecting the time represented as a long. This is not a problem in Chrome. The Date object accepts a string in the format given above, but when I try to use getTime() on a date instantiated with a string in Firefox, it returns NaN. So what I've done in the on the page I linked to is a little handling asking the user to re-enter the dates if its read as NaN. This obviously isn't even a band-aid solution since even if you re-enter the date its still going to read NaN. I need to know why the Date function wont instantiate using the string you see in the input text field in Firefox.
In IE, for some reason its telling me that sTime is undefined.
That date format is ambiguous. Try it as yyyy-mm-dd instead of mm-dd-yyyy or dd-mm-yyyy.
Try
new Date(Date(dateString)).getTime()
(feels like an ugly workaround...)
Edit: This will produce wrong result.
The date format used in Javascript should be of the form YYYY MM DD HH:mm:ss. You can convert the format into this form with
// dateString = "07-08-2010 23:28";
dateString = dateString.replace(/(\d+) (\d+) (\d+)/, '$3-$1-$2');
But as mentioned in the comment, there is no standard Date format used by Javascript before the ECMAScript 5 standard. It is better to parse the dateString directly:
m = dateString.match(/(\d+)-(\d+)-(\d+) (\d+):(\d+)/)
date = new Date(+m[3], m[1]-1, +m[2], +m[4], +m[5]); // Note: January = 0.