You need to sign in to do that
Don't have an account?
IntroAmmy
date format in lightning component
Is there any way to accept dd/mm/yyyy format date in lightning component
date field
<lightning:input aura:id="birthdate" label="DOB="{!v.leadObj.HealthCloudGA__BirthDate__c}"
type="date" displayDatePicker="true"
required="true" onchange="{!c.changeDate}" format="mm/dd/yyyy" dateStyle="short" />
I also checked with format="dd/mm/yyyy" its not working it only accepting mm/dd/yyyy
tried also datestyle attribute in component as short but accepting same mm/dd/yyyy
.js
where i am calulating age based on DOB
var birthday = component.find("birthdate").get("v.value");
if(birthday !=null || birthday!=undefined)
{
var optimizedBirthday = birthday;
}
else{
alert("Enter correct date format");
}
// it will accept two types of format yyyy-mm-dd and yyyy/mm/dd
//var optimizedBirthday = birthday.replace(/-/g, "/");
//set date based on birthday at 01:00:00 hours GMT+0100 (CET)
var myBirthday = new Date(optimizedBirthday);
// set current day on 01:00:00 hours GMT+0100 (CET)
var currentDate = new Date().toJSON().slice(0,10)+' 01:00:00';
// calculate age comparing current date and borthday
var myAge = ~~((Date.now(currentDate) - myBirthday) / (31557600000));
// alert(myAge);
Thanks,
date field
<lightning:input aura:id="birthdate" label="DOB="{!v.leadObj.HealthCloudGA__BirthDate__c}"
type="date" displayDatePicker="true"
required="true" onchange="{!c.changeDate}" format="mm/dd/yyyy" dateStyle="short" />
I also checked with format="dd/mm/yyyy" its not working it only accepting mm/dd/yyyy
tried also datestyle attribute in component as short but accepting same mm/dd/yyyy
.js
where i am calulating age based on DOB
var birthday = component.find("birthdate").get("v.value");
if(birthday !=null || birthday!=undefined)
{
var optimizedBirthday = birthday;
}
else{
alert("Enter correct date format");
}
// it will accept two types of format yyyy-mm-dd and yyyy/mm/dd
//var optimizedBirthday = birthday.replace(/-/g, "/");
//set date based on birthday at 01:00:00 hours GMT+0100 (CET)
var myBirthday = new Date(optimizedBirthday);
// set current day on 01:00:00 hours GMT+0100 (CET)
var currentDate = new Date().toJSON().slice(0,10)+' 01:00:00';
// calculate age comparing current date and borthday
var myAge = ~~((Date.now(currentDate) - myBirthday) / (31557600000));
// alert(myAge);
Thanks,
This is known behavior with lightning:input tag and it results in 3 different formats.
In order to resolve the issue, you can go with ui:inputDate like below: Refer this code as well if you want to try other options: JS controller:
Hope above information helps, Please mark as Best Answer so that it can help others in the future.
Thanks.
try following code.
let me know if it helps you and don't forget to marking it as best answer so that it can helps to others in future.
Thank you