Concern regarding the duration setting in the date-picker

Is there a specific "campaign duration" rule where you must select a date between the first and last day of the month? If you choose a date outside of this range, such as a date from the next month, the Backend API will return an "Input param error."

Currently, when selecting the last date within the date selector (the 30th day of the month), the Backend API returns an "Input param error." This indicates that it counts the 30th day as the 1st day of the following month. I need to adjust the date selection to be one day less.

The question is, where in the code should I subtract one day from the "endDate" property?

This is the HTML snippet for the modal containing the date picker: (You can see the endDate there, I want to subtract one day from that endDate)

 <!-- startDate/endDate -->
        <div class="nano-f-r nano-f">
            <nano-date-picker [(startDate)]="campaignDto.dtoData.startDate"
                              [(endDate)]="campaignDto.dtoData.endDate"
                              [intervalMode]="campaignDto.id === 'new' ? dpIntervalFuture : dpIntervalBoth"
                              [maxIntervalDurationInMonths]="2"
                              style="height: 30px"></nano-date-picker>
        </div>

nano-date-picker component:

   public isDaySelected(day: MonthDay): boolean {
    return this.isDateRange === true
        ? day.startDate === this.datePicker.startDate || day.endDate === this.datePicker.endDate
        : day.startDate === this.datePicker.startDate;
}

public ngOnChanges(): void {
    const startDate = this.startDate ? moment(this.startDate).format() : moment().startOf('day').format();
    const endDate = this.endDate ? moment(this.endDate).format() : moment(startDate).endOf('day').format();
    this.datePicker = new DatePicker(startDate, endDate);
    this.setDatePicker();

    if (this.continiousDatesValue !== null) {
        this.continiousDatesValueMoment = moment(this.continiousDatesValue);
    }
    if (this.previousDatesValue !== null) {
        this.previousDatesValueMoment = moment(this.previousDatesValue);
    }
}

private onDatesChange(): void {
    this.startDateChange.emit(this.datePicker.startDate);
    this.endDateChange.emit(this.datePicker.endDate);
    this.startEndDateChange.emit({startDate: this.datePicker.startDate, endDate: this.datePicker.endDate});
    this.isOpen = false;
}


public setRange(day: MonthDay): void {
    if (this.isDateRange === true) {
        this.datePicker.setRange(day, () => this.onDatesChange())
    } else {
        this.datePicker.setSingleDate(day, () => this.onDatesChange())
    }
}

Here is how the date picker looks like:

https://i.sstatic.net/wPpmb.png

Answer №1

To properly update the dates, make sure to subtract a day in both ngOnChanges and onDatesChange.

UPDATE

Remember to adjust your date calculation like this:

this.datePicker = new DatePicker(startDate, endDate);

Ensure the end date is adjusted correctly by subtracting one day:

endDate.setDate(endDate.getDate() - 1);
this.datePicker = new DatePicker(startDate, endDate);

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

Using Yii2, create a button with an onclick event that executes a JsExpression

I need to submit a form with an array of elements whose number is unknown. class DynamicForm extends Model { /** var string[] */ public elements[]; } In the view where the form submission takes place, I want to include a button that triggers a Ja ...

When I include scroll-snap-type: y; in the body tag, the scroll-snapping feature does not function properly

Hey there! I've been trying to implement scroll-snapping on my project but unfortunately, I couldn't get it to work. I tested it out on both Chrome and Firefox, but no luck so far. Here's the code snippet I've been working with, would a ...

Execute tap() function without subscribing

Can the tap() pipe function be executed without subscribing to it? observer$:BehaviorSubject<number[]> = new BehaviorSubject<number[]>([1]) getData(page:number):void{ of(page).pipe( tap({ next: (data) => this.observe ...

Update the CSS variables in Angular Material version 15 to reflect new changes

Angular Material 15 introduces some significant changes to colors and spacing. As a result, I find myself needing to override the default CSS variable values in my styles.scss file. :root { --mdc-typography-button-letter-spacing: 'normal'; ...

retrieve the position of a descendant element in relation to its ancestor element

I'm encountering a challenge while attempting to solve this issue. I have elements representing a child, parent, and grandparent. My goal is to determine the offset position of the child (positioned absolutely) in relation to the grandparent. However, ...

Developing a Highchart Graph using JavaScript

I am trying to develop a high chart graph. Check out my code on FIDDLE LINK. I have two values, a and b, for the x-axis and y-axis. The issue arises when the difference between a and b is minimal, such as when both are equal (-9). In such cases, the grap ...

Manipulate the css pseudo-element :after using jQuery's .siblings() method

How can I use jQuery to edit the :after element created by CSS? <div class="box"> <h3 class="social">Social</h3> <ul> <li><a href="https://www.youtube.com/" onmou ...

Generate a customizable button in Bootstrap using JavaScript extensions

After some investigation on Bugzilla, I discovered that by setting the options URL in install.rdf to arbitrary JavaScript, it can run smoothly. However, a strange issue arises where the window deactivates, as if an invisible dialog has appeared over it, ma ...

Unable to select multiple checkboxes as they are unresponsive and not registering any clicks

I am facing an issue with rendering multiple checkboxes in a grid cell. I attempted to use formly multicheckbox, but unfortunately the checkboxes did not render properly. So I decided to try a different approach, however, now the checkboxes are not being c ...

The view of the Google map is filled with numerous rounded tiles

Map divided into 3x3 tiles I am looking to customize the appearance of my map and remove all spaces and rounded corners to create a seamless, undivided visual. Is there a way to modify a map tile attribute to achieve this effect? Below is the code snippet ...

Problem encountered when Next.js and CSRF token interact on the server

I've integrated the next-csrf library (https://github.com/j0lv3r4/next-csrf) into my next.js app to safeguard api routes. Despite following the documentation, I'm encountering a 500 error with the following message: {"message":"Si ...

Route-based Dynamic Div ID Retrieval

Can you extract the ID from route.queryparams in Vue.js? <div id="{{this.$route.query.data}}" class="col-md-12>{{data}}</div> ...

Display all nested <ul> and <li> elements inside an <li> tag by utilizing jQuery

I have a complex nested tree structure of 'ul' and 'li' elements. In one instance, I need to display specific 'li' items based on a certain name while hiding the rest. Here's an example of the tree: branch1 twig1 l ...

Circular dependency situation encountered in Angular 2 shared module

I'm currently working on a share module setup that is structured as follows: @NgModule({ exports: [ CommonModule, HttpModule, OneModule, TwoModule ] }) export class SharedModule { } The OneModule imports the SharedModule in order ...

Interacting between Angular controllers and services to efficiently showcase JSON information

Recently, I started working with Angular 1.5+ and I'm facing some challenges with the basics, particularly when it comes to displaying data from a JSON file on the DOM. Although I can fetch the data successfully (at least I think so, as it console lo ...

What is the best way to add 1 to a number every second with javascript?

My code seems to be functioning correctly, but I'm having trouble setting the delay and stopping the increment after a certain interval. Can someone please assist me with this? Javascript: $(document).ready(function() { var number = parseInt($(& ...

Numerous titles being showcased

I need to enhance the functionality of clicking on multiple objects in order to display all titles at once within the header. Currently, only one title is shown each time an object is clicked. Therefore, when clicking on items 1, 2, and 3, I expect to se ...

Tips for comparing a variable within a list of objects

Here is a list I am working with: $scope.Document.push( { 'Id': '' 'Name': "", 'mandatory': "false", 'status': "1" }, { 'Id': '' ...

Understanding how to utilize and manipulate this JSON data using JavaScript

In my code, there is a variable that contains the following data: { "Rows": [ { "New":1, "CachedNumberType":0, "Date":1327479615921, "Type":2, "Number":"123456", "Duration ...

Angular button will be disabled if the form control is empty

Is there a way to deactivate the search button when the text box is empty? I attempted to use the "disabled" attribute on the search button, but it didn't work. Here is my HTML code: <div class="col-md-5 pl-0"> <div class="in ...