0

I have to convert a date format to iso format. It works fine but until the date 30. If i use 31/08/2019 it shows range error: Invalid time value at Date.toISOString. Using date picker for this. This error shown for 31 of every month.

    function utcDate(element) {
    let val = element.val();
    let split = val.split(/\//);
    var t= split[0]++;
    return new Date([split[1], split[0], split[2]].join('/')).toISOString();
} 
  • It possible that split[0] is the day of month. if today is 31 and you sum is 32 > invalid day? may you need sum split[1]++ ? – toto Jul 16 '20 at 16:19
  • while using above function without using split[0]++ output shows one date before like if i give start date as 30/08/2019 and end date as 31/08/2019 it shows start date as 29/08/2019 and end date as 30/08/2019. what to do? – LVK Entertainment Jul 16 '20 at 16:53
  • Can you givme an example the element value that you are passing? example: 31/08/2019 ? – toto Jul 16 '20 at 16:59
  • as shown above i have used 30/08/2019 and 31/08/2019 as start and end date in a form – LVK Entertainment Jul 16 '20 at 17:27
  • Could you tell me whats wrong with the above code and how your code works? – LVK Entertainment Jul 16 '20 at 17:38
  • View here the Date object constructor: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date And month in javascript first is 0 for january month, and las 11 for december. – toto Jul 16 '20 at 17:43

1 Answers1

0

try it for test mode:

                function utcDate(element) {
                    let val = element.val();
                    let split = val.split(/\//);
                    var d = new Date();
                    d.setMonth(parseInt(split[1], 10) - 1);
                    d.setDate(parseInt(split[0], 10));
                    d.setFullYear(parseInt(split[2], 10));
                    return d.toISOString();
                }
toto
  • 1,180
  • 2
  • 13
  • 30