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;
}
3
  • As @timolawl mentioned, you have to change your dates to ["2016-04-28", "2016-04-29", "2016-04-30", "2016-04-31"]. Commented May 3, 2016 at 3:07
  • @starky—you have misread timolawl's answer. The month number is changed in parsing, not in the input. Commented May 3, 2016 at 5:49
  • @RobG Yeah, sure, you're right. Commented May 3, 2016 at 8:07

3 Answers 3

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');

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

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

2 Comments

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).
Thanks Rob for testing. I have tried with Chrome which worked nicely in the end. Good to know it is prolematic with some browsers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.