Allow DateTime to have a starting date without setting the object's value
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
Added a fix to datetime to default to the current date/time or the max value if provided.
@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?
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
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
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"
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
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>
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).
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?
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 (initialValue)="2017-10-18" should be initialValue="2017-10-18" or [initialValue]="'2017-10-18'". Try that and see if it works.
@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'
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>
initialValue="2017-10-18" doesn't work for me also
are you sure you're running ionic 3.7.1? https://github.com/ionic-team/ionic/blob/master/CHANGELOG.md
ionic info
you are right, I am not using 3.7.1. Thanks
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 can you please show us an example about how you set the initial value via @ViewChild? Thx a lot!
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();
}
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.