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

Allow DateTime to have a starting date without setting the object's value

Open mikewagz opened this issue 9 years ago • 51 comments

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

I'm submitting a ... (check one with "x") [X] feature request

Current behavior: Currently to have the DateTime object open with a value (other than the max value) you must set the ngModel value for the object before the DateTime is opened. Sometimes this isn't desired, such as when you'd like to leave the field blank if the user does not input a date.

Expected behavior: Adding parameters to start the DateTime at a certain date, like today, will enhance usability. For example, if my date selection range is from 2000 - 2025, but the users most often select a date within a few weeks of today, it would be much easier to start the DateTime interface with today's date instead of 1-1-2025.

Steps to reproduce: Open DateTime.

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.4
Ionic CLI Version: 2.1.17
Ionic App Lib Version: 2.1.7
Ionic App Scripts Version: 0.0.48
ios-deploy version: 1.9.0 
ios-sim version: 5.0.11 
OS: OS X El Capitan
Node Version: v6.9.2
Xcode version: Xcode 8.2.1 Build version 8C1002

mikewagz avatar Jan 02 '17 21:01 mikewagz

Added a fix to datetime to default to the current date/time or the max value if provided.

mlynch avatar Sep 29 '17 15:09 mlynch

@mlynch it’s not enough. There cases when we neeed a specific date by default that is not today or max.

For example by default set reminder in 1 month. Today and max are not applied here.

Can we have possibility to set default explicitly?

trakhimenok avatar Sep 29 '17 16:09 trakhimenok

Okay that seems reasonable. Anyways, now the picker will open to the current time by default or the max value if it's before the current time

mlynch avatar Sep 29 '17 16:09 mlynch

As there are valid use cases for using min, max, or the current time, you could have convenience values to pass in. So...

pickerPlaceholder="2017/10/1"
pickerPlaceholder="min" // only if a min is defined, otherwise default to now
pickerPlaceholder="max" // only if a max is defined, otherwise default to now
pickerPlaceholder="now" // maybe not necessary if this is the default anyway

WhatsThatItsPat avatar Sep 29 '17 16:09 WhatsThatItsPat

Fixed in 3.7.1. Should satisfy all of the above. use initialValue to set the initial value, but note that will only "commit" if the user chooses something and hits "done"

mlynch avatar Sep 29 '17 20:09 mlynch

That’s not accurate, it should work as a plain property...

On Sun, Oct 1, 2017 at 12:32 PM MikeCIDFG [email protected] wrote:

Thanks mlynch - works perfectly!

To clarify for those Googling this but still new to how Ionic & Angular work, use (initialValue) ... Using initialValue alone doesn't work.

<ion-datetime displayFormat="MM/DD/YYYY" formControlName="someDate" (initialValue)="10/01/2017">

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/ionic-team/ionic/issues/9846#issuecomment-333392650, or mute the thread https://github.com/notifications/unsubscribe-auth/AAArzuBLHf-SSgJJyKgsmFEoL82l8Fy0ks5sn8yXgaJpZM4LZN2O .

--

Max Lynch Co-founder of Ionic [email protected] 414-530-9687

mlynch avatar Oct 01 '17 22:10 mlynch

Well, it doesn't in my case. Only event binding with parentheses (as shown below) works for me. Plain property or [bound] property default it to "01/01/2017" same as not doing anything ...

"Banana Box" two-way binding [(initialValue)]="10/10/2017" throws a template parse error on encountering =. Not sure what that's about, because nothing else changes. I'm probably just not using that right.

Might be a quirk of my setup, or using Reactive form instead of template-driven form?

In any case, the following works as expected for me w/Ionic 3.10.3, and I thought that note might be of help to others if they encounter the same issue:

<ion-datetime displayFormat="MM/DD/YYYY" formControlName="resultsReceivedDate" (initialValue)="getLocalDateTime()"></ion-datetime>

MikeCIDFG avatar Oct 02 '17 13:10 MikeCIDFG

Try an ISO formatted date: initialValue="2017-10-10".

@mlynch, Any way it could be smart enough to pick up on your displayFormat or pickerFormat?


...or a note in the docs saying it must be ISO (never mind, it's already there).

WhatsThatItsPat avatar Oct 02 '17 13:10 WhatsThatItsPat

I'm having an issue with initialValue and using displayFormat/pickerFormat to allow the user to only adjust the times.

<ion-datetime displayFormat="h:mm a" pickerFormat="h:mm a" [(ngModel)]="entry.time_end" (ionChange)="updateTimes()" initialValue="2017-10-05T04:15:48+00:00">

If the item is empty and you pick a time, it'll save just the h:mm and not in the valid Date format. But if there's an existing time, it'll save the thing in the correct format.

time_start: "2017-10-02T01:37:53+00:00",
time_end: "04:15"

Maybe I'm doing it wrong?

J-who avatar Oct 05 '17 05:10 J-who

initialValue doesn't work for me. My code is:

<ion-datetime cancelText="Cancelar" doneText="Ok" (initialValue)="2017-10-18" [(ngModel)]="editCorretiva.dataFalha" displayFormat="DD MMM YYYY"></ion-datetime>

I've tried using initialValue as a plain property, tried using a function that returns the date string and also tried to put the value format as displayFormat instead of ISO but none of them work. Any ideas?

tamiris-fonseca avatar Oct 18 '17 12:10 tamiris-fonseca

@tamiris-fonseca (initialValue)="2017-10-18" should be initialValue="2017-10-18" or [initialValue]="'2017-10-18'". Try that and see if it works.

zakton5 avatar Oct 18 '17 14:10 zakton5

@zakton5 none of them work. initialValue="2017-10-18" has no effect and [initialValue]="'2017-10-18'" returns a runtime error, it says initialValue is not a know property of 'ion-datetime'

tamiris-fonseca avatar Oct 18 '17 14:10 tamiris-fonseca

I have the following Code and it is not working too:

              <ion-datetime #dueDatePicker hidden="true" min="{{ minDate }}" [initialValue]="2017-12-30" max="2100-10-31" pickerFormat="DD MMMM YYYY HH mm" [(ngModel)]="dueDate" ></ion-datetime>

d4ex avatar Oct 26 '17 06:10 d4ex

initialValue="2017-10-18" doesn't work for me also

kukipei avatar Oct 26 '17 09:10 kukipei

are you sure you're running ionic 3.7.1? https://github.com/ionic-team/ionic/blob/master/CHANGELOG.md

ionic info

tobbbe avatar Oct 26 '17 09:10 tobbbe

you are right, I am not using 3.7.1. Thanks

kukipei avatar Oct 26 '17 09:10 kukipei

I'am using 3.7.1... but initialValue is not working because its only a solution for kukipei's problem. But via

@Viewchild

and "setValue" i can set the date to a initial value.

d4ex avatar Oct 26 '17 13:10 d4ex

@d4ex can you please show us an example about how you set the initial value via @ViewChild? Thx a lot!

fspugna avatar Oct 26 '17 14:10 fspugna

I have a Datepicker like this one in the html file: <button ion-button icon-only id="btn_add_duedate" (click)="addDueDate()"> <ion-icon ios="md-time" md="md-time"></ion-icon> <ion-datetime #dueDatePicker hidden="true" min="{{ minDate }}" cancelText="{{ 'TASK_DETAILS.ADD_DUE.CANCEL' | translate }}" doneText="{{ 'TASK_DETAILS.ADD_DUE.DONE' | translate }}" max="2100-10-31" pickerFormat="DD MMMM YYYY HH mm" [(ngModel)]="dueDate" (ionChange)="dueDateChange()"></ion-datetime> </button>

In the typescriptfile i use the following code to get the Viewchild: @ViewChild('dueDatePicker') dueDatePicker: DateTime;

... and before open of the datePicker is called i use 'setValue' to set a initialValue:

addDueDate() {
    if(this.task.dueDate != undefined) {
      this.dueDatePicker.setValue(this.task.dueDate.toISOString());
    }

    this.dueDatePicker.open();
  }

d4ex avatar Oct 30 '17 09:10 d4ex

I am using ionic 3.10.3 <ion-datetime pickerFormat="DDDD MMMM D YYYY" placeholder="Click to select date" formControlName="{{aQuestion.questionId}}" (initialValue)="10/01/2017"></ion-datetime> The initial date is still 01/01/2017

I also tried both initialValue="2017-10-18" or [initialValue]="'2017-10-18'" which didn't work.

whitecat avatar Nov 21 '17 22:11 whitecat