Ionic-framework: cannot read year of null in DateTime.calcMinMax

Created on 3 Jan 2017  路  8Comments  路  Source: ionic-team/ionic-framework

Ionic version: (check one with "x")
[ ] 1.x
[x] 2.x

I'm submitting a ... (check one with "x")
[x] bug report
[ ] feature request
[ ] support request => Please do not submit support requests here, use one of these channels: https://forum.ionicframework.com/ or http://ionicworldwide.herokuapp.com/

Current behavior:
On Android 5 when I'm trying to open datepicker I see:

TypeError: Cannot read property year of null DateTime.calcMinMax

Expected behavior:
No exception. datepicker is shown

Steps to reproduce:
pass non-empty non date value in max input of ion-datetime component

Other information:
date time util sometimes can return null (https://github.com/driftyco/ionic/blob/master/src/util/datetime-util.ts#L197) but DateTime doesn't handle this https://github.com/driftyco/ionic/blob/master/src/components/datetime/datetime.ts#L761

Ionic info: (run ionic info from a terminal/cmd prompt and paste output below):

Cordova CLI: 6.4.0 
Ionic Framework Version: 2.0.0-rc.3
Ionic CLI Version: 2.1.18
Ionic App Lib Version: 2.1.9
ios-deploy version: 1.9.0 
ios-sim version: 5.0.8 
OS: OS X El Capitan
Node Version: v6.2.1
Xcode version: Xcode 8.2.1 Build version 8C1002

Most helpful comment

Yes, worked for me but why ionic has made it so hard to use simple date picker?

All 8 comments

Hi there. Can you please provide some example code for use to test against? Something we can copy/paste and get the error. thanks

@mhartington the simplest reproduction is when somebody accidentaly pass date object into max or min inputs: http://plnkr.co/edit/qlWl7J?p=preview

In that example, you're using a javascript Date, but looking at the datetime docs, it needs an ISO formate date.

http://ionicframework.com/docs/v2/api/components/datetime/DateTime/#datetime-data

http://plnkr.co/edit/WQF3VyEL3nIQbdcNt2xf?p=preview

You're feeding it something that it is not designed handle.

Shouldn't it tell me where I'm wrong instead of failing with runtime exception? The logic may be a bit more complicated and min/max can be determined in runtime as well

max format YYYY-MM-DD fixed issue for me ex. max="2020-10-31" see http://ionicframework.com/docs/api/components/datetime/DateTime/#datetime-data

i fixed the issue by this method (for restricting the date that can be selectable to todays date)

wrote the below code in constructor
this.date=new Date();
this.maxSelectabledate=this.formatDate(this.date);

code for format date
formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();

        if (month.length < 2) month = '0' + month;
        if (day.length < 2) day = '0' + day;

        return [year, month, day].join('-');
    }

in the html
use displayFormat="YYYY-MM-DD" [max]="maxSelectabledate" in the iondatetimetag

Yes, worked for me but why ionic has made it so hard to use simple date picker?

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.

Was this page helpful?
0 / 5 - 0 ratings