javascriptdatesharepointclient-side-scriptingjslink

new Date() always read dd/mm/yyyy as mm/dd/yyyy(month first) hence day and month values get mixed resulting in NaN/Error


When converting date for example:

var dateObj = new Date("10/01/2019");
console.log(dateObj);

returns Tue Oct 01 2019 00:00:00 i.e. it takes in the day as month and likewise with the month value

How to make new Date() to take dd/mm/yyyy ??


Solution

  • Answer is here: https://stackoverflow.com/a/33299764/6664779

    (From original answer) We can use split function and then join up the parts to create a new date object:

    var dateString = "23/10/2019"; // Oct 23
    
    var dateParts = dateString.split("/");
    
    // month is 0-based, that's why we need dataParts[1] - 1
    var dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]); 
    
    document.body.innerHTML = dateObject.toString();