0

I have the following code:

var value_ = "2017-06-01";

value_ = value_.split(".")[0];
value_ = value_.replace(" ","T");
var dateObject = new Date(value_);

formattedDate = (dateObject.getMonth() + 1) + "/" + dateObject.getDate() + "/" + dateObject.getFullYear();

alert(formattedDate);

JSFiddle here:

It's printing wrong date, why?

Dan
  • 443
  • 1
  • 7
  • 19
  • `Date.parse` doesn't know how to interpret your input string. It's inverting the month and day since `06-01` is ambiguous (is the date June 1st or January 6th?). – André Dion Jun 02 '17 at 16:32
  • It's converting the date (which it interprets as UTC) to your local time zone. See the dupe. – Heretic Monkey Jun 02 '17 at 16:33
  • @AndréDion It's June 1st. – Dan Jun 02 '17 at 16:33
  • Why are you trying `.split(".")[0]` and `.replace(" ","T")`? – Washington Guedes Jun 02 '17 at 16:33
  • @WashingtonGuedes Because in some cases I am using the same code and I have date value in the form of timestamp and Internet Explorer doesn't parse milliseconds and shows NAN – Dan Jun 02 '17 at 16:34
  • @Dan It's June 1st... somewhere in the world, but not everywhere. – Joseph Marikle Jun 02 '17 at 16:34
  • @JosephMarikle True. What would be the best thing to do then? – Dan Jun 02 '17 at 16:35
  • @Dan, no, it's both. `2016-06-01` is June 1st if your input string is formatted as YYYY-MM-DD, but it's January 6th if your format is YYYY-DD-MM. – André Dion Jun 02 '17 at 16:35
  • @AndréDion That's not how the Date constructor parses dates. All, please read the duplicate I commented. It shows how to remedy this situation. – Heretic Monkey Jun 02 '17 at 16:37
  • @MikeMcCaughan Is correct. The solution in this case would be to pass three parameters to a `new Date()` object (adjusting for the month offset). – Joseph Marikle Jun 02 '17 at 16:41
  • Try [`moment.js`](https://momentjs.com/), some [examples here.](https://stackoverflow.com/documentation/momentjs/1982/getting-started-with-momentjs/18251/general-usage#t=201706021642370210783) – Washington Guedes Jun 02 '17 at 16:43

2 Answers2

-1

If you have to construct a date object from a string, use Date.parse(); with the string in the correct format. Don't pass the string directly to the Date() constructor.

// Use correct string format (this assumes that you have no control over the
// string's format).
var dateString = "2017-06-01";

// That format could be invalid, so we need to correct it.
var dateObject = new Date(dateString.replace("-", " "));

console.log("The date is: " + dateObject);

formattedDate = (dateObject.getMonth() + 1) + "/" + dateObject.getDate() + "/" + dateObject.getFullYear();

console.log(formattedDate);

However, if you want the current date, just instantiate a new date with no parameters:

var today = new Date();
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • Explain the down vote please. – Scott Marcus Jun 02 '17 at 16:33
  • I didn't downvote, but it's using the Date constructor on a string, not as numbers, so this doesn't make any sense. – Heretic Monkey Jun 02 '17 at 16:34
  • I was trying to fix my timestamp related issue since that was not included in the answer. So, now, this code works in IE 11 and it shows NAN in Chrome. https://jsfiddle.net/439rwgk3/ – Dan Jun 02 '17 at 16:52
  • @Dan You are doing too much manipulation to the string. This works: https://jsfiddle.net/73tye5cg/1/ – Scott Marcus Jun 02 '17 at 16:56
  • @ScottMarcus Thanks for answer. But that doesn't works in IE 11. It shows NAN and that's why I was manipulating – Dan Jun 02 '17 at 16:58
-2

If you want to print today's date, here is one approach:

var dateObject = new Date();

formattedDate = (dateObject.getMonth() + 1) + "/" + dateObject.getDate() + "/" + dateObject.getFullYear();

alert(formattedDate);
glhrmv
  • 1,712
  • 1
  • 16
  • 23