3

Hi Currently I am facing issues in the "Max" attribute in lightning-input but max attribute is not working properly in some cases.

My requirement is to disable dates after One year of the Current date.

I got Full date in 11 months but I want the full date in 12 months so that I can give to the max attribute

Below is the attached Screenshot -

enter image description here

Here is the Playground link for your reference

Here is the code below

HTML :-

<template>

 <lightning-input label="From Date" type="date" name="fromDate"
                                    value={fromDateValue}
                                    message-when-bad-input="Entered Date value is not a valid Date"
                                    onchange={fromdatevalueonchange} placeholder="dd MMM, yyyy" max={fulldate}
                                    message-when-range-overflow="Entered Date value is not a valid Date" required>
                                </lightning-input>     
</template>

And JS Code :-

import { LightningElement } from 'lwc';

export default class LightningExampleInputDate extends LightningElement {
connectedCallback(){
     var x = 12; //or whatever offset
        this.CurrentDate = new Date();
       console.log("Current date:", this.CurrentDate);
       this.CurrentDate.setMonth(this.CurrentDate.getMonth() + x);
       console.log("Date after " + x + " months:", this.CurrentDate);
       this.fulldate = this.CurrentDate.getFullYear() +'-'+this.CurrentDate.getMonth()+'-'+this.CurrentDate.getDate();

       console.log("full date --"+this.fulldate);
}
}
  • Do you want it to be a fiscal year or the calendar year? – rahul gawale Dec 30 '19 at 16:23
  • 1
    @rahul calendar year – Sahil Malhotra Dec 30 '19 at 17:55
2

The issue is here this.CurrentDate.getMonth().

The month starts with 0 and not 1.

0 -> Jan
1 -> Feb
.
.
10 -> Nov
11 -> Dec

Src: https://www.w3schools.com/jsref/jsref_getmonth.asp

Thus to fix your code, you have to add 1 in your month when you create full date.

this.fulldate = this.CurrentDate.getFullYear() +'-'+(this.CurrentDate.getMonth()+1)+'-'+this.CurrentDate.getDate();

Playground Link: https://developer.salesforce.com/docs/component-library/tools/playground/o3_Lrf0N/4/edit

| improve this answer | |
  • 2
    Thank you so much mate. It works :) – Sahil Malhotra Dec 30 '19 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.