What is the best method for synchronizing the end_date picker with the start_date picker in an ionic2 app and vice versa

My goal is to dynamically adjust the end date picker based on the user's selection of the start date, and vice versa if the user decides to choose the end date first. Here is the code I have written:

    <ion-item>
      <ion-label>Start Date</ion-label>
      <ion-datetime displayFormat="MMM DD, YYYY" pickerFormat="MMM DD YYYY" ngControl="start" [(ngModel)]="start_date"></ion-datetime>
   </ion-item>
   <ion-label>{{getDate(start_date | date:'MMddy')}} </ion-label>
    <ion-item>
      <ion-label>End Date</ion-label>
      <ion-datetime displayFormat="MMM DD, YYYY" pickerFormat="MMM DD YYYY" ngControl="end" min="getDate(start_date | date:'MMddy')" ></ion-datetime>
    </ion-item>

The start-date is initialized in the controller as new Date(), and the getDate function transforms the date format accordingly:

   getDate(date){
     date = date.split('\/');
     date = date[2] + '-' + date[0] + '-' + date[1];
     return date;
   }

The label displayed using the code above shows the correct result: 2016-29-06 (today's date). However, the end date picker does not work with this logic. If I manually set the minimum date for the end datetime property like this, it works:

  <ion-datetime displayFormat="MMM DD, YYYY" pickerFormat="MMM DD YYYY" ngControl="end" min="2016-06-29" ></ion-datetime>

Answer №1

If you want to set the start date as the minimum, you can create a separate method for retrieving and storing it:

getStartDate(){
    var minDate = getDate(start_date | date:'MMddy');
    return minDate;
}

Then modify min to use this new method:

min="getStartDate()"

Answer №2

Success! I found the solution by adding curly braces {{}} to the min expression. Another option is to use [min] instead of min.

<ion-item>
      <ion-label>Start Date</ion-label>
      <ion-datetime displayFormat="MMM DD, YYYY" pickerFormat="MMM DD YYYY" ngControl="start" [ngModel]="start_date" (ngModelChange)="start_boolean=true" min="{{getDate(today | date:'MMddy')}}"></ion-datetime>
   </ion-item>
    <ion-item *ngIf="start_boolean">
      <ion-label>End Date</ion-label>
      <ion-datetime displayFormat="MMM DD, YYYY" pickerFormat="MMM DD YYYY" ngControl="end" min="{{getDate(start_date | date:'MMddy')}}"></ion-datetime>
    </ion-item>

Additionally, in the TS file:

I initialized the today variable as follows:

today: Date = new Date();

and created the getDate function:

   getDate(date){
     date = date.split('\/');
     date = date[2] + '-' + date[0] + '-' + date[1];
     let re = /\//gi;
     date = date.replace(re, "-");
     return date;
   }

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

Exploring cutting-edge Angular 2 UI controls?

Imagine this scenario: An extensive organization is in need of developing a large web application with advanced UI components, such as hierarchical grid/tree and charts, alongside the standard UI elements. All these controls should ideally be sourced fro ...

The application's functionality is interrupted when router.navigate() is called within the .subscribe method

I am having an issue with user navigation on my application. After successfully signing in, users get redirected to the home page (/), but they are unable to navigate by clicking any links on that page. Upon further investigation, I discovered that moving ...

Bring in a class using an NPM package that contains several classes

When attempting to import classes from my NPM package in main.js, I encountered an issue. import { Organization, Club } from "sqorz-client"; o = new Organization(); o.setId("id"); c = new Club(); c.setId("anotherid"); The er ...

Hostlistener is unresponsive to inputs from arrow keys

Within my Angular 2 component, I have implemented a key event listener to capture keystrokes: @HostListener('document:keypress', ['$event']) handleKeyboardEvent(event: KeyboardEvent) { console.log(event); } Oddly enough, I am not ...

Using React FC with functions is a great way to enhance the functionality

I am eager to learn how to implement React.FC<> in regular functions on react.js. There are two types of functions I am aware of; the first being (which I personally prefer): function Welcome(props) { return <h1>Hello, {props.name}< ...

Issue: The data type 'void' cannot be assigned to the data type 'ReactNode'

I am encountering an issue while calling the function, this is just for practice so I have kept everything inside App.tsx. The structure of my class is as follows: enum Actor { None = '', } const initializeCard = () => { //some logic here ...

Passing HttpClient from app.module to another module and then to a service in Angular

Currently, I am in the process of developing my own Angular NPM Package with the prefix "ngx-*". I have successfully compiled the package and am now integrating it into a new project by utilizing the npm link command. Within the service, there is a constr ...

Ways to address conflicts with npm peer dependencies

I am in the process of upgrading my Angular version from 11 to 14. Almost all of the peer dependencies have been resolved successfully, except for one remaining issue. npm ERR! Found: @angular/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" ...

Tips for managing numerous nested subscriptions

Looking to extract the id parameter from the route, fetch the corresponding task, and then its parent if applicable. Angular CLI: 7.1.4 Node: 11.6.0 OS: linux x64 Angular: 7.1.4 @angular-devkit/architect 0.11.4 @angula ...

Sending selected IDs from the JSON data

In my project, there is a JSON file named "workers" which contains information about all the workers. I have created a select component to display the names of the workers like this: https://i.sstatic.net/0Glyf.png Currently, I am selecting some workers ...

Ensuring Angular animations run smoothly

When using ngx-toastr with angular/animations for displaying toasts, I noticed a significant delay in the animation when triggering them from a manually created event. export class AppComponent { @ViewChild('iframeElement') iframeElement ...

Exploring Angular 2 Tabs: Navigating Through Child Components

Recently, I've been experimenting with trying to access the HTML elements within tabs components using an example from the Angular 2 docs. You can view the example here. Here is a snippet of my implementation: import {Component, ElementRef, Inj ...

Using Angular's routerLink feature to assign a specific outlet (version 5.0.2)

Despite reading numerous posts on this problem, none of the solutions seem to work for me. I am working with one app module, one routing module, and no additional modules. The issue I'm facing is that... Standard routes can be linked from any compo ...

Angular 6 Bootstrap Fixed Positioning

Looking to implement affix or scrollspy functionality from Bootstrap 4 into Angular 6, I've been searching for existing libraries. Came across JonnyBGod's scrollspy, but it appears to be designed for Angular 5 and uses rxjs 5. Does anyone know of ...

Error compiling SCSS in Angular 6 due to a util.js issue

As a novice in the world of Angular 6, I am currently exploring the Angular CLI and trying to grasp the file structure. My goal is to utilize SCSS for creating a unified global stylesheet. However, during compilation, an error keeps popping up: ERROR in . ...

What is the process for subscribing to the output of a flatMap operation?

Is there a way to subscribe to the result of flatMap in this code block? timer(0, 2000) .pipe( flatMap(() => this.scannerService.scan(this.scanType)), takeWhile(res => res.errorDesc !== "SUCCESS") ) .subscrib ...

What is the best way to prevent double clicks when using an external onClick function and an internal Link simultaneously

Encountering an issue with nextjs 13, let me explain the situation: Within a card component, there is an external div containing an internal link to navigate to a single product page. Using onClick on the external div enables it to gain focus (necessary f ...

Angular router.redirect is not able to pass variables as expected

I am facing an issue with the router redirect and template lifecycle while using Stripe checkout in my Angular 5 project. After a user signs up, I trigger the stripe checkout modal. Once the modal receives a payment source token, I perform some additional ...

ControlValueAccessor failing to populate with data from external service

After realizing the previous question was slightly off track, I am focusing on creating a custom component that can be bound to a FormControl within a FormGroup. I have successfully made it work for user input using CVA, but facing issues when preloading ...

What is the best way to send an object to an Angular form?

I am facing an issue with my Spring entity, Agent, which includes an Agency object. When adding a new agent, I need to pass the agency as an object in the Angular form. While the backend code is functioning correctly, I am struggling to figure out how to p ...