1

I need to calculate the date range using date format d/m/y using javascript. example: 20/1/2020 until 21/1/2020 is a 1-day difference.

select 8th January 2020

but when I console it shows as "Sat Aug 01 2020 00:00:00 GMT+0800 (Singapore Standard Time)" and if I pick 13/01/2020 its will come out as an invalid date.

How can I change the date format from m/d/Y to d/m/Y because this will affect my calculation?

Thank you in advance.

junk food
  • 11
  • 2

1 Answers1

0

If you direly need to pass the date in as 'd/m/y' you can just write a factory function for Dates:

var getDate = function(dateString) { 
   var dateParts = dateString.split('/');
   var datePartsTmp = dateString.split('/');
   dateParts[0] = datePartsTmp[1];
   dateParts[1] = datePartsTmp[0];
   return dateParts.join('/');
};
console.log(getDate('13/01/2020'));
gabriel.hayes
  • 2,267
  • 12
  • 15