Shows date as NAN in IE only

I have this function defined in my Javascript code and it shows NAN in IE only and works fine in all browser:

function formatDateString(value_) {

                      var formattedDate = "";
                      if (!isEmpty(value_)) {
                          var dateObject = new Date(value_);
                          formattedDate = (dateObject.getMonth() + 1) + "/" + dateObject.getDate() + "/" + dateObject.getFullYear();
                      }
                      return formattedDate;
}

After reading online I saw people discussing that IE expects year first but if I keep my year first then my date format will change, right?

To help us troubleshoot the problem, what values are you giving the function?

Sorry for not providing that information, I am providing the following value :

2017-02-13 04:33:00.0

Using Date.parse(“2017-02-13 04:33:00.0”) I’m shown that it’s NaN.
Using Date.parse(“2017-02-13 04:33:00”) without the millisecond results in a correctly parsed date.

Therefore, get rid of the milliseconds and you’ll be fine.

Thanks. Do you think getting rid of millisecond using javascript would do if it’s possible in the function somewhere or should I make changes in the webservice ?

If you have no requirement for the milliseconds, which is most likely to be true, adjusting the web service config is the most preferred path to success.

Do you think it can be fixed using the Javascript only and without modifying the webservice?

Yes indeed, just split it based on the fullstop and take the left part of the split.

value_ = value_.split(".")[0];

Hi Paul,

It’s actually showing NAN after removing the milliseconds as well. Please see this JSFiddle example in IE.

Thanks

Make it a proper DateTime string matching ISO 8601 by replacing the space with a T, and it will then work.

value_ = value_.replace(" ", "T");

That’s a pretty common error for IE. Dates are terrible in IE.

I don’t remember this being an issue post-IE8, though.

Thanks very much. That worked . Appreciated !

Thanks for the link.

Hey Paul, I was actually testing this because the solution we discussed was not giving me today’s date. It was printing yesterday’s date. So, I have the following which works fine in IE 11 and shows NAN error in Chrome. Any idea why that could be happening? Here is my JSFiddle.

https://jsfiddle.net/439rwgk3/

dateString.replace("-", " ")

Why are you doing that? Leave the dashes.

1 Like

So you are saying I should just do this :

var dateObject = new Date();

Or Please suggest if any other thing you meant in your last reply?

No. Just drop the replace that gets rid of the dashes.

var dateObject = new Date(dateString)

@mawburn I did as you mentioned. But for this scenario, I am getting yesterday’s date. Please find the JSFiddle below:

https://jsfiddle.net/439rwgk3/2/

But it works fine and shows June 1 with this format 2017-06-01 04:33:00.0

Dates are wonky. You need to throw a 00:00:00 timestamp on it as well to make sure it’s parsed as a local timestamp. It shows you yesterday, because it’s calculating off of 2017-06-01T00:00 UTC and using your local timezone to display it. So if you’re West of UTC, you’ll have -X to the time. If you look in the console, it’ll show you what your timezone is.

Is this for school or study? If it isn’t and you’re going to keep throwing different formats, timezones, and whatever else… you should probably just use moment.js.

This is for my work. I think it would be best to change from the backend to throw full timestamp then.

Haven’t tried using momemt.js. I will check that out. Do you think that should help in resolving the same issue without making changes from the back end?