ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

ionic2 - unable to update date in datetimepicker

Open tobika opened this issue 10 years ago • 15 comments

When I was changing just the day for example in the datepicker nothing changed and the console logged

Error parsing date: "[object Object]". Please provide a valid ISO 8601 datetime format: https://www.w3.org/TR/NOTE-datetime

As I looked in the code I found that there are missing conditions in the updateDate function in datetime-utils.js

else if ((isPresent(newData.year) || isPresent(newData.hour))) {
            if (isPresent(newData.ampm) && isPresent(newData.hour)) {
                if (newData.ampm.value === 'pm') {
                    newData.hour.value = (newData.hour.value === 12 ? 12 : newData.hour.value + 12);
                }
                else {
                    newData.hour.value = (newData.hour.value === 12 ? 0 : newData.hour.value);
                }
            }
            for (var k in newData) {
                existingData[k] = newData[k].value;
            }
            return;
        }
        console.warn(`Error parsing date: "${newData}". Please provide a valid ISO 8601 datetime format: https://www.w3.org/TR/NOTE-datetime`);

As you see the date is only changed if either the year or the hour has changed. I guess that was forgotten?

I could do an PR and add month, day, minute, and second if needed. Used this fix locally for a fast workaround.

tobika avatar Sep 29 '16 13:09 tobika

@tobika Please go ahead with the PR! Can you provide a test that fails without your fix? I am not sure what bug you are trying to fix.

Thanks!

manucorporat avatar Sep 30 '16 22:09 manucorporat

Just to explain better, the bug appears in two configurations on your example page http://ionicframework.com/docs/v2/demos/src/datetime/?production=true&ionicplatform=mdl

When you try the "Chrome" example on this page you can only change month and day (same in my app), when the updateDate function runs it checks if either the year or hour parameter exists, if not (like in this case) it logs a warning

Same for the "alert" example where you only choose the minutes and nothing else.

I would say that we should at least check month, day and minutes. Will add the tests(there are no tests yet for this function) that fail and then make a fix/

tobika avatar Oct 01 '16 10:10 tobika

@tobika did you solve this issue? i have the issue too

adirzoari avatar Oct 06 '16 11:10 adirzoari

@adirzoari @manucorporat yes, there is a PR waiting with the correction #8387

tobika avatar Oct 06 '16 11:10 tobika

@tobika sorry i'm new in github,where is it? i check in the #8387 and i don't see the answer for that.. do you use also ionic2?

adirzoari avatar Oct 06 '16 11:10 adirzoari

@adirzoari if you click on the link I just posted you can switch to Files Changed to see the new code that has been added to fix the problem

As a workaround you can just go into the file /node_modules/ionic-angular/util/datetime-utils.js

and change the in the updateDate function the following code:

-    } else if ((isPresent(newData.year) || isPresent(newData.hour))) {
+    } else if ((isPresent(newData.year) || isPresent(newData.hour) || isPresent(newData.month) || isPresent(newData.day) || isPresent(newData.minute) || isPresent(newData.second))) {

Remove the line with the - and add the line with +

tobika avatar Oct 06 '16 11:10 tobika

@tobika you use ionic 2 also?

adirzoari avatar Oct 06 '16 11:10 adirzoari

@adirzoari yes

tobika avatar Oct 06 '16 11:10 tobika

Merged!

manucorporat avatar Oct 12 '16 21:10 manucorporat

@tobika have u solve this , i have a same problem

manikandan07081995 avatar Jan 09 '18 13:01 manikandan07081995

@manikandan07081995 could you give more info which error you have? this PR was merged and normally everything should work as this bug was fixed

tobika avatar Jan 09 '18 13:01 tobika

@tobika issue closed.

manikandan07081995 avatar Feb 23 '18 17:02 manikandan07081995

Hi, @tobika. I have same issue, im getting a date with ngModel, but when i set this date (with ion-datetime) this error appear:

datetime-util.js:229 Error parsing date: "Wed Feb 28 2018 09:56:12 GMT-0300 (Hora oficial do Brasil)". Please provide a valid ISO 8601 datetime format: https://www.w3.org/TR/NOTE-datetime.

Has this problem been resolved yet?

Thanks in advance

edisonmatoso avatar Feb 28 '18 13:02 edisonmatoso

Hi @edisonmatoso this is not a problem with the datetimepicker, you have to give the correct date data to the ngmodel. The error message gives you the details becaus your format you are trying to pass is not valid Wed Feb 28 2018 09:56:12 GMT-0300 (Hora oficial do Brasil). It should be the ISO 8601 format: 2011-10-05T14:48:00.000Z

tobika avatar Mar 02 '18 10:03 tobika

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

ionitron-bot[bot] avatar Sep 01 '18 08:09 ionitron-bot[bot]