Chrome & Firefox Date function different. (new Date) - javascript

new Date
when I use the date function in chrome I get something like this
Thu Jul 16 2015 20:37:47 GMT-0700 (Pacific Daylight Time)
when I do the same function in firefox I get this
2015-07-17T03:29:03.110Z
why are they different? I googled for a bit and search on stack but even the examples they were using to show their problem were comparing two dates of the same format. What I'd really like to know is why are they different now and how can I go about making them the chrome format cross as many browsers as possible?
Thanks

Resolved with moment.js
Firefox is in fact giving a different date string, and not simply displaying is differently as stated by Xufox.
The function I am going to use is
moment(new Date()).format("YYYY MMM D H:mm:ss")
but I will change around the .format, just wanted to get the answer here in case others run into the same problem
EDIT!!!!:
Xufox was right. I was a dummy. I had to manually edit some code in my timeline feed to get it going. I needed to use Date.parse on my generic time string, do my editing to the time, then use new Date. Thanks Xufox.

Related

Apply timezone to timestamp

I have an api which is currently returning a UTC timestamp in the following format
2022-09-11T14:29:55.343Z
And In my frontend application, if I call .toUTCString() I get the following
Sun, 11 Sep 2022 14:29:55 GMT
Which is nice and “human readable”
From the same api, I am can also get a “timezoneOffset” which I need to apply to this if it is present, and that comes back as a string in the following format
"+02"
(note that this can also come in minus formats also)
My first reaction is to substring the first element of the above to find out if it’s a + or -, and then perform the relevant operation with the “number” part.
Very roughly, that would give me something like this (with rawDate being the value at the top of the question, and the + and number being worked out from the timezoneOffset value)
const newDate = new Date(rawDate.setHours(rawDate.getHours() + number)).toUTCString())
which will render out the following
Sun, 11 Sep 2022 16:29:55 GMT
The time is now obviously correct, but as this is a modified version of the UTC timestamp, the time zone hasn’t really been applied.
is there a built in way to “apply” a time zone to a timestamp which is in UTC format, or is there a library that I can use with the above vales? I started to look into moment.js but a lot of the posts around that seem to suggest it is now deprecated, and it feels a little overkill as my application only has a few places where data formatting is needed.

Date differences between Chrome & Firefox

I am getting an odd difference between Firefox and Chrome using the same bit of code:
var d = new Date('2019', '4', '4');
In Chrome I get the expected result: May 04 2019
In Firefox I get the following result: 2019-05-03
Why is Firefox 1 day out?
Here are some screenshots from the consoles in both browsers:
Chrome:
Firefox:
Those are the same date/time. The difference is that the first one is being shown to you in BST (British Summer Time, GMT+0100), and the second one is being shown to you in GMT (the Z on the end tells you that).
The Date constructor constructs dates in local time, which for you apparently is currently BST (for me, too :-) ). Since you haven't specified a time, it defaults to midnight. Firefox just shows you that in GMT, which is an hour earlier than midnight BST, hence the previous day.
Side note: The arguments you provide to new Date should be numbers, not strings. Although the date constructor will coerce for you, it's best practice not to rely on it.

Safari and Chrome showing different results of new Date() [duplicate]

I want to convert date string to Date by javascript, use this code:
var date = new Date('2013-02-27T17:00:00');
alert(date);
'2013-02-27T17:00:00' is UTC time in JSON object from server.
But the result of above code is different between Firefox and Chrome:
Firefox returns:
Wed Feb 27 2013 17:00:00 GMT+0700 (SE Asia Standard Time)
Chrome returns:
Thu Feb 28 2013 00:00:00 GMT+0700 (SE Asia Standard Time)
It's different 1 day, the correct result I would expect is the result from Chrome.
Demo code: http://jsfiddle.net/xHtqa/2/
How can I fix this problem to get the same result from both?
The correct format for UTC would be 2013-02-27T17:00:00Z (Z is for Zulu Time). Append Z if not present to get correct UTC datetime string.
Yeah, unfortunately the date-parsing algorithms are implementation-dependent. From the specification of Date.parse (which is used by new Date):
The String may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the String. The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.
To make the Date constructor not (maybe) use the local timezone, use a datetime string with timezone information, e.g. "2013-02-27T17:00:00Z". However, it is hard to find a format that is reliable parsed by every browser - the ISO format is not recognised by IE<8 (see JavaScript: Which browsers support parsing of ISO-8601 Date String with Date.parse). Better, use a unix timestamp, i.e. milliseconds since unix epoch, or use a regular expression to break the string down in its parts and then feed those into Date.UTC.
I found one thing here. It seems the native Firefox Inspector Console might have a bug:
If I run "new Date()" in the native Inspector, it shows a date with wrong timezone, GMT locale, but running the same command in the Firebug Extension Console, the date shown uses my correct timezone (GMT-3:00).
Noticed that FireFox wasn't returning the same result as Chrome. Looks like the format you use in kendo.toString for date makes a difference.
The last console result is what I needed:
Try using moment.js. It goes very well and in similar fashion with all the browsers. comes with many formatting options. use moment('date').format("") instead of New Date('date')

How to validate date in chrome browser as well as in firefox browser

In chrome I am trying to check date if it is valid date or not.
In have a variable:
var d = new Date('9/'); // Not a correct format
In Chrome
console.log(new Date('9/')); // Output: Sat Sep 01 2001 00:00:00 GMT+0530 (India Standard Time) Which is wrong as I am providing wrong date.
In Firefox
console.log(new Date('9/')); // Output: Invalid Date
Also using date.toLocaleString() has output 06 May 2007 00:00:00 in firefox
and in chrome it is 1/9/2001;
So my question is how to validate date in chrome and keep code safe from these differences. If need more details about it please write in comment.
The Javascript Date API can have somewhat mixed results between browsers, as you've discovered.
The best option I can suggest is to use one of the third-party libraries that are available for this. There are two I can recommend -- either Moment.js or Date.js.
Both of these libs have much better validation and parsing, and are more consistent cross-browser than the built-in Date class.
The correct answer is to use a Javascript library as #Spudley suggested like moment.js.
However, if you don't want to include that awesome library (or another like it), you could try running your date through a regex like this:
var date = "9/",
pattern = /(?:(?:\d{2}[-\/]){2}\d{4})|(?:\d{4}(?:[-\/]\d{2}){2})/,
isValid = false;
isValid = pattern.test( date );
This will match yyyy/mm/dd, dd/mm/yyyy, mm/dd/yyyy and yyyy/dd/mm (plus a number of variations [and issues]).
However, this is extremely naive, doesn't match the vast majority of date formats, allows mixed separators, and a wide array of other issues.
Again, I strongly recommend you implement a library like moment.js, but this regex might be a start.

Javascript Date.parse bug when dash-delimited and starts with year

Am seeking confirmation if this is a bona fide documentation and/or implementation bug with Javascript's Date.parse method.
The docs I'm referring to are at https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/parse and they say 'If you do not specify a time zone, the local time zone is assumed.'
But the following code shows that, despite not specifying a time zone, local time is not being assumed (but rather my timezone offset is being applied), if the string passed to Date.parse begins with the 4-digit year representation, and is dash-delimited.
var euroStyleDate = '2011-10-04';
var amerStyleDate = '10/04/2011';
var euroStyleParsed = Date.parse(euroStyleDate);
var amerStyleParsed = Date.parse(amerStyleDate);
console.log(euroStyleParsed); //1317686400000
console.log(amerStyleParsed); //1317700800000
console.log(new Date(euroStyleParsed));
//Date {Mon Oct 03 2011 20:00:00 GMT-0400 (Eastern Daylight Time)}
console.log(new Date(amerStyleParsed));
//Date {Tue Oct 04 2011 00:00:00 GMT-0400 (Eastern Daylight Time)}
There may even be other cases, and I'm sure I'm not the first to discover this if I am incorrect. So beyond confirmation, I'd surely love to be pointed at more in-depth information on this if anybody knows of pertinent links.
I'm experiencing this in FF3, Chrome for Windows and of course just to be special IE8 doesn't even seem to able to perform the conversion on 2011-10-04 whatsoever: I'm just getting an empty string in my application
Thanks in advance for any further insight or resources.
I ran into this concept, too. For anyone googling "Javascript dates dashes slashes" like I was, this is the clearest demonstration that I can think of as to what's going on here.
In short, slashes means local time zone, and dashes means UTC. Other answers has explanations regarding why.
<script type="text/javascript">
var
testB = new Date("2012/02/09"),
testC = new Date("2012-02-09");
alert(testB.toString());
alert(testC.toString());
alert(testC.toUTCString());
</script>
**Update:**It looks like there are several different standards at work here:
The EMCAScript < 5 standard allowed for dates in the standard IETF format, e.g. Sun Oct 03 2010. With these dates, the local timezone is assumed.
In ECMAScript 5, a limited version of the ISO 8601 standard is also allowed, e.g. 2010-10-03. The spec seems to say (perhaps following ISO 8601?) that in this case, the UTC timezone is assumed if one is not specified.
I haven't found a spec that says Date.parse can handle mm/dd/yyyy dates, but my browser (Chrome 14) clearly can, and probably other browsers can too. This appears to follow standard 1 above, assuming the local timezone. Given that it's not in the spec, however, I would recommend not using this version, as it's likely to be browser-dependent (and I have no idea whether 10-03-2010 would result in a different date if I had a European locale set on my browser).
There are a few issues with the native Date.parse function in most interpreters - I have often had timezone issues like the one you describe. So in general, I either use a library like Datejs or I write my own parsing functions. The DateTime module of the SIMILE AJAX library has a pretty good example function for parsing ISO-8601 dates (what you're referring to as euroStyleDate, plus an optional time component).
When setting dates, I generally use new Date() and then use the setUTC*() functions to set the different date elements to my desired precision. It's not perfect, but at least you're dealing with a clear timezone.

Categories

Resources