getTime in chrome and firefox different result - javascript

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

Related

Date Definition in Edge, Chrome and Firefox

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!

Unable to parse the date

I'm trying to parse the date which is in the following format
dateFormat: "d-M-y" // returns 10-Oct-13
I'm using jQuery UI for formatting date.
Below is my code:
var d1 = new Date("10-Oct-13");
alert(d1); //Invalid in FF and IE, works in chrome
Seems weird, here is my JSFiddle for reproducing the bug in FF and IE.
Note: I don't want to use plugin, since it is working chrome.
Please share your thoughts.
You can use Datepicker's parseDate() method in conjunction with the format string to parse the date:
var d1 = $.datepicker.parseDate("d-M-y", $("#lastAssimilationDate").val())
alert(d1); // alerts: Thu Oct 10 2013 00:00:00 GMT+0200
See the edited JSFiddle.
From the MDN doc for Date:
dateString
String value representing a date. The string should be in a format recognized by the parse method (IETF-compliant RFC 2822 timestamps).
Essentially you're passing a string in an unsupported date format as the dateString parameter of the constructor, so the JavaScript engine is (correctly) stating that it's an invalid date. Chrome seems to be slightly more forgiving with the date formats it allows, but that's non-standard.
You can use the getDate function to obtain a Date object representing your selected date:
var d1 = $('#lastAssimilationDate').datepicker("getDate");
Use the built-in getDate method:
$('button').click(function(){
var d1 = $("#lastAssimilationDate" ).datepicker('getDate');
console.log(d1);
});
You can also assign an altField with an altFormat of yyyy-mm-dd if you need to send an ISO-standard date to the server.

Javascript invalid Date on iphone

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().

Invalid date [dd/mm/yyyy] in safari? How to display in dd/mm/yyyy format using jquery

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/

Date (dateString) returning invalid date in Firefox

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.

Categories