function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sue Irvine 16Sue Irvine 16 

lwc date format in recordeditform

Hello - I have a LWC with a RecordEditForm and want to set the value of a custom field to today's date upon submit, but can't get the right formatting in the Javascript submit handler. Everything I've tried gives me an invalid date format error. Can someone clue me in on the right format to use? Thanks!
 
handlesubmit(event){
        event.preventDefault();  
        console.log('handle submit');
        const fields = event.detail.fields;
        fields.Travel_Dates_Sent_to_PS__c = false;
        if (this.updatetype=="cancel"){
            let dt = Date.now();
            fields.Travel_End_Date__c=dt;
        }
        this.template.querySelector('lightning-record-edit-form').submit(fields);
    }

 
Best Answer chosen by Sue Irvine 16
Maharajan CMaharajan C
Hi Sue,

Please use the below code to acheiev this:

Use the seperate JS method to get the date in YYYY-MM-DD format then you can save the date in custom field.
 
handlesubmit(event){
        event.preventDefault();  
        console.log('handle submit');
        const fields = event.detail.fields;
        fields.Travel_Dates_Sent_to_PS__c = false;
        if (this.updatetype=="cancel"){
			let todaydate = this.formatDate();
            fields.Travel_End_Date__c=todaydate;
        }
        this.template.querySelector('lightning-record-edit-form').submit(fields);
    }

    formatDate() {
        console.log('formatDate');
        let d = new Date();
        let month = '' + (d.getMonth() + 1);
        let day = '' + d.getDate();
        let year = d.getFullYear();

        if (month.length < 2) 
            month = '0' + month;
        if (day.length < 2) 
            day = '0' + day;
    return [year, month, day].join('-');
    }

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Sue,

Please use the below code to acheiev this:

Use the seperate JS method to get the date in YYYY-MM-DD format then you can save the date in custom field.
 
handlesubmit(event){
        event.preventDefault();  
        console.log('handle submit');
        const fields = event.detail.fields;
        fields.Travel_Dates_Sent_to_PS__c = false;
        if (this.updatetype=="cancel"){
			let todaydate = this.formatDate();
            fields.Travel_End_Date__c=todaydate;
        }
        this.template.querySelector('lightning-record-edit-form').submit(fields);
    }

    formatDate() {
        console.log('formatDate');
        let d = new Date();
        let month = '' + (d.getMonth() + 1);
        let day = '' + d.getDate();
        let year = d.getFullYear();

        if (month.length < 2) 
            month = '0' + month;
        if (day.length < 2) 
            day = '0' + day;
    return [year, month, day].join('-');
    }

Thanks,
Maharajan.C
This was selected as the best answer
Tofing ali Tofing aliTofing ali Tofing ali
All the survey participants get a free entry in the sweepstakes event on completing the survey successfully. Well, it is necessary to fulfill some requirements and follow the simple survey steps as shown below to get free entry.

Tell Pizza Hut Survey (https://papasurvey.info/tellpizzahut-com-tell-pizza-hut-survey/)
Sue Irvine 16Sue Irvine 16
Thank you Maharajan, that worked!
Maharajan CMaharajan C
Hi Sue,

Can you please mark the best answer to close this thread ...

Thanks,
Maharajan.C
Lindsay Holmes 8Lindsay Holmes 8
An easier and faster way to do this is by using toISOString().
fields.Travel_End_Date__c = new Date().toISOString();

 
Victor LockwoodVictor Lockwood
@Lindsay - Thank you! In my case, I'm not using the lightning record edit form, but I am using updateRecord from uiRecordApi and .toISOString() did the trick!