0

I'm try to initialize a new date object but I don't understand why returns me ever Invalid Date.

var dateString= this.get("created_at");
    var dataParts = dateString.split(' ');
    var timeParts = dataParts[3].split(':');

    //console.log(dataParts);-->["Fri", "May", "09", "17:45:54", "+0000", "2014"] 
    //console.log(timeParts);-->["17", "45", "54"]


    var year=dataParts[5];
    var month=dataParts[1];
    var day=dataParts[2];
    var hour=timeParts[0];
    var minute=timeParts[1];
    var second=timeParts[2];


    var date = new Date(year,month,day,hour,minute,second);


    console.log(date);
2
  • ["Fri", "May", "09", "17:45:54", "+0000", "2014"] Should be ["Fri", "04", "09", "17:45:54", "+0000", "2014"] Commented May 9, 2014 at 22:37
  • 2
    Please read the documentation: "month Integer value representing the month, beginning with 0 for January to 11 for December." Commented May 9, 2014 at 22:37

1 Answer 1

3
var date = new Date(year,month,day,hour,minute,second);

using this constructor the month needs to be a number, not "May".

Note also that months begin at 0 for January, so May is 4.

Alternatively, construct the date as a string: new Date("May 09, 2014 17:45:54") from the parts that you have. (You won't have to split the time or lookup the month-number.)

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

3 Comments

+1 - Small note, months should be 0 based, so May should be 4.
Could you tell me how convert may in appropriate number?
Use an Object as a lookup table. {..., May: 4, ...}

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.