1

I'm having problems with date string formatting using Javascript. On IE the date format is MM-DD-YYYY but on Firefox it's YYYY-MM-DD. Chrome works both of them but when I choose one of the formats, the other browser gives me an Invalid Date warning.

My code as an example (works on IE but not on Firefox):

  var dateSTR = "2015-09-29";
  var date  = new Date(dateSTR.replace(/(\d{4})-(\d{2})-(\d{2})/,"$2-$3-$1"));
  date.setUTCHours(date.getHours());
  date.setUTCMinutes(date.getMinutes());
  return date.toLocaleDateString(); 

https://jsfiddle.net/kmmna8c0/

Jonathan
  • 32,202
  • 38
  • 137
  • 208
Kamuran Sönecek
  • 3,293
  • 2
  • 30
  • 57
  • I want to find a solution without a plugin. – Kamuran Sönecek Sep 29 '15 at 07:13
  • 1
    "On IE date format is MM-DD-YYYY" is a very broad statement. In what way is that "the date format"? The parameter to `new Date(dateString)` is documented to be in ISO-8601 or RFC2822 format, so I'd expect you to just be able to use `dateSTR` directly, without any messing around. – Jon Skeet Sep 29 '15 at 07:14
  • 1
    possible duplicate of [Javascript Date() constructor doesn't work](http://stackoverflow.com/questions/163563/javascript-date-constructor-doesnt-work) – CBroe Sep 29 '15 at 07:16
  • _“I want to find a solution without a plugin”_ – well then write code that parses your input date format into its single parts yourself, and then feed them to the Date constructor version that takes single arguments for each one of them. – CBroe Sep 29 '15 at 07:18
  • @JonSkeet when I use dateSTR directly, it does not work on IE. CBroe thanks for your link – Kamuran Sönecek Sep 29 '15 at 07:22
  • "It does not work" doesn't give us much information... what happens? – Jon Skeet Sep 29 '15 at 07:24
  • See also: http://stackoverflow.com/questions/2182246/javascript-dates-in-ie-nan-firefox-chrome-ok – Jon Skeet Sep 29 '15 at 07:28
  • @JonSkeet there is a jsfiddle link. Just warn that "Invalit Date". You can change format and test it on IE & FF – Kamuran Sönecek Sep 29 '15 at 07:29
  • @KamuranSönecek: Providing a jsfiddle link doesn't help if we don't have access to IE, and don't even know which version of IE you're having problems with. It's surely much better to give a clear explanation of what you're seeing on which browser version. – Jon Skeet Sep 29 '15 at 07:29

4 Answers4

2

This code will return you the same date in IE, Chrome and FireFox:

var dateSTR = "2015-09-29";
var dateArr = dateSTR.split("-");
var theDate = new Date(dateArr[0], dateArr[1]-1, dateArr[2]);
alert(theDate);
Aleksei Golubev
  • 342
  • 3
  • 11
1

It seems like you are trying to create a Date object from a predefined string (in your example: "2015-09-29").

You can use the following:

var dateSTR = "2015-09-29";
var dateYear = 2015;         // create this from dateSTR
var dateMonth = 8;           // create this from dateSTR (= month-1)
var dateDay = 9;             // create this from dateSTR

and then use:

var date = new Date(dateYear, dateMonth, dateDay);

based on the explanation here: http://www.w3schools.com/jsref/jsref_obj_date.asp

Miki Berkovich
  • 467
  • 5
  • 16
1

Split it like this

var args = dateStr.split(/[-/]/); // Add any special characters if you need.

And create date from it like this

var d = new Date(args[0], args[1] - 1, args[2]);

Thats it.

Exception
  • 8,111
  • 22
  • 85
  • 136
  • @JonSkeet What now? For that small change, it does not deserve down vote – Exception Sep 29 '15 at 07:51
  • 1
    Why not? It confidently ("that's it") provided an answer that didn't work. I'd say that makes the answer "not useful", which is the tool tip for the downvote button... especially as optic had already provided similar but *working* code 10 minutes earlier. I've removed the downvote now that it's fixed, but I think it's entirely reasonable to downvote broken answers. – Jon Skeet Sep 29 '15 at 07:53
  • @JonSkeet Ok I agree with the comment you made if its matching question requirement, but the question is about logic to parse the values. And the focus of my answer was providing better parsing logic.. Later he can do whatever the operation he want to do with the date. `And this question is not about date. Its about parsing the date`. – Exception Sep 29 '15 at 11:05
  • And parsing the date to the wrong value is unhelpful, IMO. I'm pretty sure an implicit requirement of the question is that the `Date` obtained for "2015-09-29" is September 29th 2015, not January 1st 2001 for example... – Jon Skeet Sep 29 '15 at 11:11
  • @JonSkeet Skeet `everybody. I had a error and cofused because of it. The problem occured because of date string format. On IE date format is MM-DD-YYYY and on Firefox it's YYYY-MM-DD. Chrome works both of them but when I choise one of the formats, the other browser give me Invalid Date warning.` It's because he is not parsing it properly. – Exception Sep 29 '15 at 11:14
  • This is my last comment - I don't believe this comment thread is particularly helpful. (Arguing over downvotes rarely is - and this is arguing over a *now-removed* downvote, at that.) The OP wasn't able to correctly parse their string as a Date. Your initial answer *also* didn't correctly parse the string as a date, because it got the month wrong. I think that's unhelpful, hence worthy of a downvote to help differentiate it from the answers which gave the correct answer. Now that the answer has been fixed, I've removed the downvote. I really don't see the problem. – Jon Skeet Sep 29 '15 at 11:19
  • Ok. I am fine with the down vote that was given as the proper description was given in comment. Cheers. – Exception Sep 29 '15 at 11:23
1

I only see a difference in the toLocaleDateString - NOTE: I am in Europe, so only FX is showing me what I want

    var dateSTR = "2015-09-29";
    var date  = new Date(dateSTR.replace(/-/g,"/"));

    console.log("0:"+ new Date("2015/09/29"))
    console.log("1:"+date)
    date.setUTCHours(date.getHours());
    console.log("2:"+date)
    date.setUTCMinutes(date.getMinutes());
    console.log("3:"+date);
    console.log("4:"+date.toLocaleDateString()); 

Chrome:

0:Tue Sep 29 2015 00:00:00 GMT+0200 (W. Europe Daylight Time)
1:Tue Sep 29 2015 00:00:00 GMT+0200 (W. Europe Daylight Time)
2:Mon Sep 28 2015 02:00:00 GMT+0200 (W. Europe Daylight Time)
3:Mon Sep 28 2015 02:00:00 GMT+0200 (W. Europe Daylight Time)
4:9/28/2015

IE10:

0:Tue Sep 29 00:00:00 UTC+0200 2015 
1:Tue Sep 29 00:00:00 UTC+0200 2015 
2:Mon Sep 28 02:00:00 UTC+0200 2015 
3:Mon Sep 28 02:00:00 UTC+0200 2015 
4:28 September 2015 

FX:

"0:Tue Sep 29 2015 00:00:00 GMT+0200" 
"1:Tue Sep 29 2015 00:00:00 GMT+0200" 
"2:Mon Sep 28 2015 02:00:00 GMT+0200" 
"3:Mon Sep 28 2015 02:00:00 GMT+0200" 
"4:28/09/2015"
mplungjan
  • 169,008
  • 28
  • 173
  • 236