1

In my angular application for date time selection I am using input type="datetime-local".

 <input id="field_bookingTime" type="datetime-local" class="form-control" name="bookingTime" [(ngModel)]="bookingTime"
   required/>

Now I need to disable the dates that are previous to current date and the dates that are 3 days next to current date. For example for min, I have added validations as shown below. But the validations are not working, still the previous dates are getting enabled in the displayed calendar.

currentDate = new Date();

     <input [min]="currentDate" id="field_bookingTime" type="datetime-local" class="form-control" name="bookingTime" [(ngModel)]="bookingTime"
       required/>
2
  • Is the input to be used in any Angular forms? Commented Jan 8, 2019 at 12:12
  • Yes, I am using angular template driven forms Commented Jan 8, 2019 at 12:18

3 Answers 3

2

I would recommend writing a custom validator for the form control. Min and max have bad browser support, this goes for datetime-local aswell though.

    function dateValidator(c: FormControl) {
        // Not sure if c will come in as a date or if we have to convert is somehow
        const today = new Date();
        if(c.value > today) {
            return null;
        } else {
            return {dateValidator: {valid: false}};
        }
    }
    ...
    myForm = this.formBuilder.group({
        date: ['', dateValidator]
    })
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

Or use the Angular Material teams datepicker alternatively
2

<input> elements of type datetime-local accepts values for min and max as string in yyyy-MM-ddThh:mm format only. Since new Date() returns a string which is not in the correct format min and max won't work. Just convert the date to the correct format like this.

currentDate = new Date().toISOString().substring(0, 16);

Here we are converting the date to the desired format first by converting it to a simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long and then removing the chars after yyyy-MM-ddThh:mm

1 Comment

Clever. This doesn't appear to work for showing the locale time tho. Only GMT?
0

Try to format your date with dashes:

Example:

pipe = new DatePipe('en-US');

minDate = new Date();
minDateOut = pipe.transform(minDate, 'yyyy-MM-dd');

maxDate = new Date(minDate.getTime() + (1000 * 60 * 60 * 24 * 3));
maxDateOut = pipe.transform(maxDate, 'yyyy-MM-dd');


<input 
    [min]="minDateOut" 
    [max]="maxDateOut" 
    id="field_bookingTime" 
    type="datetime-local" 
    class="form-control" 
    name="bookingTime" 
    [(ngModel)]="bookingTime"
    required/>

Or just use any other date format without spaces...

2 Comments

I have internationalization support for my project,so by predefining 'en-US', may effect other regions
You can get locale like this: let locale = Intl.DateTimeFormat().resolvedOptions().locale; Or Just use another method to format date string, you don't have to use DatePipe...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.