I'm working on a requirement to display current date in the date field.
In the below example, if I change defaultValue to value, I see the currentDate is being displayed on page load for date element. But even after selecting a different date, I see current date is getting displayed.
If I keep defaultValue, current date is not getting displayed. Could someone suggest on how to display currentDate on page load and the date can be changed if needed to any other date.
<
DatepickerInput
id = "run-now-date-range-from"
label = "Time frame from date"
isSrOnlyLabel = {
true
}
name = "runNowOptions.dateRange.dateRangeFrom"
defaultValue = {
currentDate
}
validate = {
value => {
let error = "";
if (!validateDate(value)) {
error = controllerData?.reportTemplates[selectedReportTemplateIndex]?.runNowOptions?.dateRange?.showFutureDate ?
"Please enter a from date up to 46 days in the future." :
"Please enter a from date.";
}
return error;
}
}
settings = {
{
endDate: getDateRange(controllerData?.reportTemplates[selectedReportTemplateIndex]),
startDate: controllerData?.reportTemplates[selectedReportTemplateIndex]?.runNowOptions?.dateRange?.showFutureDate ? "0d" : "",
}
}
/>
import React, {
InputHTMLAttributes
} from "react";
import {
InputProps
} from "./utils/input-types";
export declare type DatepickerInputProps = InputHTMLAttributes < HTMLInputElement > & InputProps & {
settings ? : any;
microcopy ? : string;
validate ? : (any: any) => string;
};
declare const DatepickerInput: React.FC < DatepickerInputProps > ;
export default DatepickerInput;
in javaScript there is a inbuild function Date()
you can give it a try
const foo = new Date();
//above statement will return the current date
const foo = new Date(2022, 08, 22);
//above statement will return the date 22/08/2022
//so you can use variable to set the date value like below
const foo = new Date(year, month , day);