-1

I am trying to parse the date to string that is input by a user using IBM's BPM Coach framework (input is in dd/MM/YYYY). I need parse the Date to String and pass it to an external service. I am getting desired results by using the following code snippet but for a few cases.

var today = new Date();
    var dd = today.getDate();

    var mm = today.getMonth()+1;
    
    var yyyy = today.getFullYear();
    if(dd<10){
        dd='0'+dd;
    } 
    if(mm<10){
        mm='0'+mm;
    } 
    
    var today = dd+'/'+mm+'/'+yyyy;
    console.log(today)

While testing I provided the value on input say 01/01/1991, the javascript parser fails and the input captured is something like 32/12/1990.

I am not sure how to cater this. Any help would be highly appreciated.

Greenstick
  • 8,632
  • 1
  • 24
  • 29
jonathan white
  • 33
  • 4
  • 15

2 Answers2

0

How would you call getDate() in this situation so it would return 32/12/1990

function getDate( inDate ) {
    var dd = inDate.getDate();
    var mm = inDate.getMonth()+1;
    var yyyy = inDate.getFullYear();

    if(dd<10) dd= '0'+dd;
    if(mm<10) mm= '0'+mm;

    return dd+'/'+mm+'/'+yyyy;
}

console.log( getDate( new Date()) );
Greenstick
  • 8,632
  • 1
  • 24
  • 29
Jarek Kulikowski
  • 1,399
  • 8
  • 9
  • Ok so i just ran another test run where i gave the value 01/01/1990 and when i see the parsed date in debugger it gave me upto milliseconds and the values that were given are as: – jonathan white Jul 21 '17 at 19:37
  • I don't know how you are calling it. Perhaps adding some code showing the way you call it to the question would help. – Jarek Kulikowski Jul 21 '17 at 19:42
  • Ok so i just ran another test run where i gave the value 01/01/1990 and when i see the parsed date in debugger it broke the date upto milliseconds and the values that were given are as: """ `1989 11 31 1 19 0 0 0` – jonathan white Jul 21 '17 at 19:44
  • 1
    @user3081780 Could you please edit your question and add this and any other relevant information to it, so people can reproduce the problem and find an answer. –  Jul 21 '17 at 20:37
0

It may come down to what browser you are using. Chrome parses (correctly by most persuasions) dates differently from Firefox and Safari, and more generally, "Yeah, unfortunately the date-parsing algorithms are implementation-dependent".

So what to do? You could try using jQuery UI's date picker and pass in the format you need, that way you can avoid the reformatting altogether and ensure that it works cross-browser.

Greenstick
  • 8,632
  • 1
  • 24
  • 29
  • I am working on IBM BPM where you could write javascript code in activities. So I dont know what is wrong with the date parser as when i see the date parsed in the debugger it gives me something like this. `1989 11 31 1 19 0 0 0` – jonathan white Jul 21 '17 at 20:18