1

I'm starting with a json string that looks like: ["2016-05-28", "2016-05-29", "2016-05-30", "2016-05-31"]

I'm trying to convert this into Saturday 5/28 Sunday 5/29.

I looked at these answers and tried to implement the same: Why does Date.parse give incorrect results? and Convert date in string to date object for inserting in database.

But I'm getting the wrong day output. 5/28 comes out as Tuesday, 5/28 when it is a Saturday.

JSFiddle: https://jsfiddle.net/pum40hyx/

Here's my code where I convert the date into my desired string:

function convertToNiceDate(inputDate)
{
    var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var splitString = inputDate.split("-");

    currentDate = new Date(splitString[0], splitString[1], splitString[2]);
    var day = currentDate.getDate();
    var month = currentDate.getMonth();

    //this is the problematic line!
    var dayOfWeek = days[currentDate.getDay()];

    var dateString = dayOfWeek + ", " + month + "/" + day;
    return dateString;
}
Community
  • 1
  • 1
Keatinge
  • 4,330
  • 6
  • 25
  • 44

3 Answers3

5

The range for months is 0-11 when constructing a new Date using the new Date(year, month[, day[, ...) format. So January should be 0, not 1 when the string is split.

month: Integer value representing the month, beginning with 0 for January to 11 for December.

Here's a hacky solution to prove the point:

function convertToNiceDate(inputDate)
{
    var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var splitString = inputDate.split("-");

    currentDate = new Date(splitString[0], +splitString[1]-1, splitString[2]);
    var day = currentDate.getDate();
    var month = currentDate.getMonth() + 1;

    //this is the problematic line!
    var dayOfWeek = days[currentDate.getDay()];

    var dateString = dayOfWeek + ", " + month + "/" + day;
    return dateString;
}

document.body.innerHTML = convertToNiceDate('2016-01-01');

You can also do the following:

function convertToNiceDate(inputDate) {
  var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  var splitString = inputDate.split("-");

  currentDate = new Date(inputDate);
  var day = currentDate.getUTCDate();
  var month = currentDate.getUTCMonth() + 1;

  //this is the problematic line!
  var dayOfWeek = days[currentDate.getUTCDay()];

  var dateString = dayOfWeek + ", " + month + "/" + day;
  return dateString;
}

document.body.innerHTML = convertToNiceDate('2016-01-01');
timolawl
  • 5,434
  • 13
  • 29
0

Use setters(maybe you should use parseInt on all of them):

var currentDate //with var
   = new Date();
   currentDate.setFullYear(splitString[0]);
   currentDate.setDate(splitString[2]);
   currentDate.setMonth(parseInt(splitString[1])-1);

   //do something with your date object
user3791775
  • 426
  • 3
  • 5
  • Answers with zero explanation can always be better by explaining how you solved the problem in words. One should not have to study code and compare it with the other code to figure out what you changed. – jfriend00 May 03 '16 at 03:14
  • This will fail more or less randomly. If run for a date like 31 July on 28 February 2017, setting the date to 31 will result in 3 May, then setting the month to July will result in 3 July, not 31 July. To avoid such issues, set all the parts in one go when calling *setFullYear*: `currentDate.setFullYear(splitString[0], splitString[1])-1, splitString[2]);` – RobG May 03 '16 at 05:53
0

I just found out that when you split the strings, the Month of your date ( here splitString[1] ) is always one too far. I think this is because it takes the array of the month ( 5 ) and this would represent June instead of May (it starts with 0 at January). Try it with:

currentDate = new Date(splitString.toString());
Tosch
  • 475
  • 5
  • 12
  • This will pass a string like "2016,05,28" as a single argument, which the Date constructor will attempt to parse. It may or may not do so correctly, but likely the result will be an invalid date (Chrome does OK, IE does not). – RobG May 03 '16 at 05:59
  • Thanks Rob for testing. I have tried with Chrome which worked nicely in the end. Good to know it is prolematic with some browsers – Tosch May 03 '16 at 12:54