Setting Max or Min Date to Today for Date Input in Angular 2 and Ionic 2

 <input class="alert-input date-input" #dob="ngModel" name="dob" max="2018-03-07" [(ngModel)]="leadDetail.dob" type="date"></div>

Is there a way to dynamically set the max date to today instead of 2018-03-07?

I have tried a couple of methods but without success:

 <input  max="today" type="date"></div>
 <input  max="{{today | date:'yyyy-mm-dd'}}" type="date"></div>

Class -

public today = new Date();

but unfortunately it did not work as expected.

Answer №1

Give this a try:

<input class="custom-input date-picker" name="dateOfBirth" [max]="currentDate" type="date">

currentDate = new Date().toISOString().split('T')[0];

See Demo Here

Explanation:

The reason for splitting the date is that using new Date() will provide you with more information than just the date, so it is necessary to extract only the date portion. To see this in action, run the following code snippet:

console.log(new Date(), '----', new Date().toISOString());

Answer №2

To modify the format from mm to MM, make sure that it is bound to another variable through ngModel for the changes to take effect on the date itself.

Answer №3

If the input date is later than the current date, the submit button will be disabled.

register.component.html

<div class="container">
<form #loginForm="ngForm" (ngSubmit)="submitLoginForm(loginForm)" style="background-  color:beige;">
<input [(ngModel)]="vdate"  name="dob"  type="date">

<button type="submit" [disabled]="!loginForm.valid || (today < vdate)" class="btn">Login</button>

</form>

</div>

register.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
vdate: Date
today = new Date().toJSON().split('T')[0];
constructor() {

}

ngOnInit() {

}
submitLoginForm() {
console.log("Welcome to Jollywood")
}
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The combination of Heroku (Cedar) with Node, Express, and Jade is causing issues with the client-side javascript files in a subdirectory. While they work fine locally with foreman and

As a newcomer to node and Heroku, I am facing what seems like a simple permission issue. Despite my efforts, I am unable to pinpoint the exact source of the problem. In a sub-directory located one level beneath my root directory where the web.js file is s ...

Utilizing AJAX to Access JSON Arrays

After clicking a button, I want to append specific parts of a JSON array (received from a server) to a table. However, I'm facing an issue where I am unable to add these parts to the table. Here is the JSON Array that was received from the server: {& ...

Error with decodeURIComponent function in Internet Explorer 8

My widget, loaded as an iframe, requires data about the hosting page. I have UTF-8 strings extracted from a page with Russian text. The page itself has proper HTML5 doctype and meta charset. This is how my code operates: params = "x1=" + encodeURICompone ...

Enhance the efficiency of synchronized horizontal scrolling using React/Redux to boost overall performance

I'm currently working on creating a grid with synchronized horizontal scrolling across different sections (such as a header and multiple table sections below). Each section should scroll together horizontally, maintaining synchronization using a redux ...

Combine multiple form values to calculate the total sum

To tackle the issue of adding up independent totals in a PHP foreach loop and displaying a grand total, we need to modify the JavaScript function. Right now, the function works fine for each line, but doesn't calculate the grand total correctly. Let&a ...

Modify the title of the audio track in videojs while utilizing HLS technology

Currently, I am implementing video.js with both DASH and HLS depending on the platform that accesses the website. The issue I am encountering is that the audio track labels in the manifest files are not being properly displayed. To address this problem, I ...

What is the reason for the lack of user data being saved in studio3t?

I'm struggling to identify the issue in my app development process. I'm creating an application that collects user email and password, storing them in a database. The problem lies in the fact that while user passwords are successfully stored, the ...

The function this.listCatModel.push is not a valid operation

I have a dropdown that I want to populate using the following code: this.categoryService.GetListItem(this.GetAllcatListUrl).subscribe(data=>{ this.listCatModel=data, this.listCatModel.push({ id:0, name:'Main Category', parent ...

Strain out specific keys from an array of objects

After utilizing the API, I receive an array of objects structured as follows: [ { id: 5, name: "foo" }, { id: 7, name: "bar" } ] My goal is to extract only the ID values and transform the array into this format: [5,7] What approach would be regarded ...

How can I access clipboard information within the ng-paste directive?

Currently, I am working with angularjs 1.3.2 and I'm seeking a way to retrieve the clipboard data during a paste event. Can anyone provide guidance on how to achieve this? This question is similar to the one found at: "Paste" event in Angul ...

Creating a standard login.js file for seamless integration with nightwatch.js testing

When creating tests for my web application, I need to first simulate a login before proceeding with the rest of the tests to access inner pages. Currently, I am in the process of refactoring the code so that I can create an 'include' for common f ...

Adding a prefix to all specified routes in Express.js

Imagine having an Express app defined in a file called server.js as follows: const app = express(); app.use('/foo', foo); app.use('/bar', bar); module.exports = app; You then import this Express app into another file named index.js: ...

Set the variable in the ajax call to the value retrieved from the previous ajax response

I am facing an issue with my ajax function that runs every 3 seconds, calling a 'file2.php' file. I pass a variable $lastid through POST to be used in 'file2.php'. The response from 'file2.php' contains a new last id which sho ...

What methods can be used to strategically incorporate gray into both the left and right sides of a page design?

I'm currently working on an asp.net MVC layout page and I'm trying to create a layout where the left side and right side are gray. However, I'm not sure how to achieve this using HTML, CSS, or Bootstrap. I specifically need to know how to ...

Narrow down an array of objects by matching a specific property and value to a separate array of objects

I am facing a challenge with two sets of arrays - one for the headers (columns) of a table and the other for the rows. const headers = [ { text: 'Dessert (100g serving)', align: 'left', sortable: false, value: 'n ...

What are some effective methods for optimizing GLSL/C code in Three.JS when using PerObject-Blur?

UPDATE 2 I have successfully integrated custom attributes using THREE.js. Each pass in the vertex shader is now aligned with the position attribute, resulting in an efficient solution with minimal code. An example will be added later UPDATE 1 This me ...

word-wrap: break-word; repair or substitute

Is there a way to prevent line breaks before words that are going to break anyway when using word-wrap: break-word;? It seems unnecessary and unattractive. Any suggestions? For Example: Consider a div with word-wrap: break-word;, set at a width equal to ...

How to display a PDF in Angular 6 using a byte array retrieved from a WebAPI

Having trouble opening a PDF from byte array sent by WebAPI. This is my Service: fetchPdfDocument(): Observable<any> { return this.httpClient .get(this.configuration.serverUrl + this.configuration.getPdfDoc, { re ...

Learning to implement the ng2-chart.js directive within Angular 2

I recently installed ng2-charts through npm First, I ran npm install ng2-charts --save and npm install chart.js --save After that, I included the following code in my index.html file: <script src="node_modules/chart.js/src/chart.js"></script> ...

The object's key values were unexpectedly empty despite containing data

There's an issue with my object - it initially gets key values, but then suddenly loses them all. All the key values become empty and a message appears in the console for the object: "this value was evaluated upon first expanding. it may have ch ...