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.
Related
I'm trying to convert javascript date strings into date objects. It seems that when I format the strings with slashes, like 2010/05/21, I get the date object I was expecting, but when I format the string with dashes, like 2010-05-21, I get a different date object which seems to refer to the previous day.
The following code illustrates my issue:
var aDate = new Date('2010-05-21')
console.log(aDate.toDateString())
console.log(aDate.toISOString())
console.log('=-=-=-=-=')
var anotherDate = new Date('2010/05/21')
console.log(anotherDate.toDateString())
console.log(anotherDate.toISOString())
The above code produces the following output:
2010-05-21T00:00:00.000Z
Thu May 20 2010
=-=-=-=-=
2010-05-21T06:00:00.000Z
Fri May 21 2010
It seems like part of the issue might be related to timezones, since getting the ISO string of the date objects shows the date objects to be 6 hours apart from each other, but I have no idea why using dashes instead of slashes would cause that. I'm using Google Chrome on MacOS Sierra, in case that's relevant.
2010/05/21 is a non-ISO date format, so support will be browser implementation dependent. Some browsers may reject it, others will accept it but use different time zones. It looks like your browser is parsing 2010/05/21 with your local time zone.
2010-05-21 is in a simplified ISO 8601 format, so ES5+ has specifications for how it must be parsed. In particular, it must assume the UTC time zone.
You can verify that it's using your local time zone by comparing it to how your browser parses an ISO 8601 date and time (which the ES5 specification says must use the local time zone).
var dateNonISO = new Date('2010/05/21');
var dateLocal = new Date('2010-05-21T00:00:00');
var dateUTC = new Date('2010-05-21');
console.log("Non-ISO:", dateNonISO.toISOString());
console.log("ISO Local:", dateLocal.toISOString());
console.log("ISO UTC:", dateUTC.toISOString());
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);
I have these 2 console logs, but they return different times (-2 hours off).
console.log(new Date()) // Date 2015-04-20T15:37:23.000Z
console.log(Date()) // "Mon Apr 20 2015 17:37:23 GMT+0200 (CEST)"
I know using Data() is the same as using the constructor and calling .toString().
However, i do need the Date() time and not the new Date() time. So why is it returning the wrong time and how can i reset it to output the correct one?
thx,
So why is it returning the wrong time and how can i reset it to output the correct one?
Your first statement is calling console.log with a Date object. It appears that whatever browser/console plugin you're using chooses to display that Date object by using an ISO-8601 string in UTC. E.g., the format is chosen by the console implementation.
Your second statement is calling console.log with a string, so the format is chosen by the Date implementation. The specification makes no requirement on what that string format must be from JavaScript engine to JavaScript engine (yes, really*) other than that it must be in the local timezone.
Apparently, on your browser, the console implementation and the Date#toString implementation don't match up.
However, i do need the Date() time and not the new Date() time.
Those strings define the same moment in time (plus or minus a couple of microseconds); it's just that the strings have been prepared with different timezone settings.
If you need to log a string to the console in local time, use
console.log(String(new Date()));
...to reliably get the string from the Date object, not something generated by the console.
* Yup, the format you get from Date#toString is undefined and entirely up to the JavaScript implementation; the only requirement is that Date() and Date.parse() can both parse the string toString outputs and get back to the original date. (JavaScript didn't even have a standard date/time format until ES5, and it only applies to the toISOString method, not toString. And ES5 got it slightly wrong and it had to be amended in ES6, leading to the unfortunate situation at the moment where Chrome does one thing and Firefox does another when parsing strings in the new date/time format with no timezone on them.)
Those are the same time. The string generated by Date() just uses a different timezone, as can be seen from the GMT+0200 (CEST) suffix.
So if you need your time string in your local timezone, just use .toString() instead of .toUTCString().
I am currently having some issues converting a string dateTime object in JavaScript
I am assuming it is because my string cannot me used properly in a new Date() but I'm not sure that is the problem.
My Input: "2011-09-29 14:58:12"
My code:
var date = "2011-09-29 14:58:12";
var added = new Date(date);
var year = added.getYear();
However, my year var contains NaN. Same with getDay() or getMonth(). What is the problem?
ps: I'm getting the date in it's format from a SQLite database. And I'm using Titanium Mobile, so javascript and SQLite are the only things involved
You're relying on the Date constructor parsing an unsupported format. Until recently, there was no standard string format supported by the Date constructor. As of ECMAScript5, there is one (YYYY-MM-DDTHH:MM:SS, note the T rather than space), but it's only been specified for just under two years and naturally doesn't work in older browsers.
For the time being, your best bet is to parse it yourself (you can find code in this question and its answers), or use something like DateJS, MomentJS, date-fns, etc. to parse it for you.
The Date constructor will not parse a string for you. You'll need to use Date.parse to do that. Interestingly enough, Date.parse doesn't actually return a Date. Instead it returns a unix timestamp. You can then pass the unix timestamp into the Date constructor to get what you're looking for.
var d = new Date(Date.parse("2011-09-29 14:58:12"));
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.