1

I'm trying to do export excel using alasql where I'm transforming the date that I'm getting from JSON ("10 Jun 2018") to MM/DD/YYYY (6/10/2018) format as shown below:

var date="12 Apr 2018";
function trandformDate(date){
return new Date(date);
}

However on export to excel I'm seeing the date 1 day less than the actual date ie.,6/9/2018enter image description here

I tried by incrementing the date by doing this..But didn't work

   var date="12 Apr 2018";
    function trandformDate(date){
    return new Date(date.setDate(date.getDate() + 1));
    }

Please let me know how can I get the exact date using javascript..thanks

forgottofly
  • 2,729
  • 11
  • 51
  • 93
  • It looks like you define the trandformDate function twice but never call it. – ale10ander May 25 '18 at 05:33
  • I haven't mentioned the call here.I have called it.. – forgottofly May 25 '18 at 05:47
  • 1
    You should not use the built-in parser for non-standard string formats, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) A "JSON" date should be in ISO 8601 extended format like "2018-05-25T06:22:10.416Z". – RobG May 25 '18 at 06:20

1 Answers1

1

Define your function once like so:

function trandformDate(dateString){ 
    var date = new Date(dateString);
    return new Date(date.setDate(date.getDate() + 1));
}

Then call it like this

var dateStr = "12 Apr 2018";
var date = trandformDate(dateStr); // Fri Apr 13 2018
ale10ander
  • 942
  • 5
  • 22
  • 42