I want to display default date values to Lightning-Input, how to get the Today Date and Current year date in Javascript. Format should be :- May 15, 2020


The following can get you current date


    var today = new Date();
    var last=new Date(new Date().getFullYear(), 11, 32);

here is a PG https://developer.salesforce.com/docs/component-library/tools/playground/9NLbuG8pA/3/edit

  • Thanks, and also i have one more date input field to default the current year end date i.e Dec 31, 2020
    – Chowdary
    May 15 '20 at 10:48

Unfortunately, the answer from @User6670 is going to give you time zone headaches where the "today" calculation is concerned. Say you have a user in California who accesses your component on May 15, 2020 at 5pm (their time). Since they have a time zone offset of 7 hours, they will see "May 16, 2020" -- the date in UTC.

To fix this, adjust for the user's local time zone:

// Get the current date/time in UTC
let rightNow = new Date();

// Adjust for the user's time zone
    new Date().getMinutes() - new Date().getTimezoneOffset()

// Return the date in "YYYY-MM-DD" format
let yyyyMmDd = rightNow.toISOString().slice(0,10);
console.log(yyyyMmDd); // Displays the user's current date, e.g. "2020-05-15"

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

  • Thanks Matthew Souther
    – Chowdary
    May 18 '20 at 2:25

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.