Just leverage the built-in toISOString method that brings your date to the ISO 8601 format:

let yourDate = new Date()
yourDate.toISOString().split('T')[0]

Where yourDate is your date object.

Edit: @exbuddha wrote this to handle time zone in the comments:

const offset = yourDate.getTimezoneOffset()
yourDate = new Date(yourDate.getTime() - (offset*60*1000))
return yourDate.toISOString().split('T')[0]
Answer from Darth Egregious on Stack Overflow
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-basic-exercise-3.php
JavaScript: Display the current date in various format - w3resource
// Get the current date var today = new Date(); // Get the day of the month var dd = today.getDate(); // Get the month (adding 1 because months are zero-based) var mm = today.getMonth() + 1; // Get the year var yyyy = today.getFullYear(); // ...
Discussions

javascript - How to get current date "YYYY-MM-DD" in string format in GEE script - Geographic Information Systems Stack Exchange
I tried to get current "YYYY-MM-DD" in Google Earth Engine script. I succeeded to get current "YYYY-MM-DD" in "Date" format in GEE script. But I want to convert it to String format for setting as a More on gis.stackexchange.com
🌐 gis.stackexchange.com
April 17, 2020
How to get yesterday's date in yyyy-mm-dd format?
This should do it: const yesterday = (new Date(Date.now() - 86400000)).toISOString().split("T")[0]; More on reddit.com
🌐 r/Frontend
10
3
March 15, 2020
Convert date in JavaScript to yyyy-mm-dd format
I possess a date string in the form Sun May 11, 2014. What is the best way to transform it into the format 2014-05-11 using JavaScript? function formatDate(inputDate) { const dateObj = new Date(inputDate); const year = dateObj.getFullYear(); const month = String(dateObj.getMonth() + 1).padStart(2, ... More on community.latenode.com
🌐 community.latenode.com
0
December 3, 2024
How get a date formatted like 2023-02-07 in JS?
You can do a quick and dirty format to yyyy-mm-dd with date.toISOString().slice(0, 10) More on reddit.com
🌐 r/webdev
26
0
March 13, 2023
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date
Date - JavaScript | MDN
YYYY is the year, with four digits (0000 to 9999), or as an expanded year of + or - followed by six digits. The sign is required for expanded years. -000000 is explicitly disallowed as a valid year. MM is the month, with two digits (01 to 12). Defaults to 01. DD is the day of the month, with two digits (01 to 31).
🌐
W3Schools
w3schools.com › js › js_date_formats.asp
JavaScript Date Formats
Independent of input format, JavaScript will (by default) output dates in full text string format: ISO 8601 is the international standard for the representation of dates and times. The ISO 8601 syntax (YYYY-MM-DD) is also the preferred JavaScript date format:
🌐
ServiceNow Community
servicenow.com › community › developer-forum › get-today-s-date-in-mm-dd-yyyy-format-without-time › m-p › 1980056
Solved: Re: Get today's date in MM-DD-YYYY format without ... - Page 2 - ServiceNow Community
January 13, 2024 - javascript var gdt = new GlideDateTime(); var date = gdt.getLocalDate(); var formattedDate = date.getMonth() + '-' + date.getDay() + '-' + date.getYear(); gs.info(formattedDate); ... - Create a new GlideDateTime object. - Use the getLocalDate() method to get the current date. - Use the getMonth(), ...
Top answer
1 of 3
9

I've had this exact same problem.

You have 3 options:

Option 1

Rely on EE to convert the date to string, but then you need to employ deferred execution in order to set it as the value of your Textbox:

var txtBox = ui.Textbox({
  // value: Today01, // will be set later, in a callback
  style: {width : '90px'},
  onChange: function(text) {
    var period_former_end = text;
  }
});

var eeNow = ee.Date(Date.now());
eeNow.format('YYYY-MM-dd').evaluate(function(dateStr){
  txtBox.setValue(dateStr);
})

print(txtBox)

The advantage is that you have a very wide flexibility regarding the format, as it relied on Joda Time.

The disadvantage is that the text is not immediately available, it needs to wait that EE makes the computation.

Option 2

Rely on JS to do the formatting, but then you need to find a format that suits your needs (see here for a list of countries and their formats). For the one that we need (YYYY-MM-DD), I have found that Canadians are pretty sane people:

var now = new Date();
var nowStr = now.toLocaleDateString('en-CA'); // thank god Canadians have a sane locale

var txtBox = ui.Textbox({
  value: nowStr,
  style: {width : '90px'},
  onChange: function(text) {
    var period_former_end = text;
  }
});


print(txtBox)

Advantage is that the date is available and displayed immediately.

Disadvantage is that you need to rely on some pre-existing format, you can't customise to your liking. Also, this may be problematic if you want a format that's not all numeric, like '2020-Mar-16', in which case the name of the month may be localised.

P.S. if you have similar formats for time, you can use now.toLocaleTimeString(locale). For example 'fr-CH' have 23:59, not AM/PM

Option 3

Do the formatting yourself. This is "imported" from this SO answer:

function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) 
        month = '0' + month;
    if (day.length < 2) 
        day = '0' + day;

    return [year, month, day].join('-');
}

Advantage is that you can code pretty much any format you want.

Disadvantage is that you have to reinvent the wheel.

2 of 3
4

This is the simplest option I can come up with:

new Date().toISOString().split('T')[0]
🌐
Reddit
reddit.com › r/frontend › how to get yesterday's date in yyyy-mm-dd format?
r/Frontend on Reddit: How to get yesterday's date in yyyy-mm-dd format?
March 15, 2020 -

I got a neat line of code to get today's date in yyyy-mm-dd format:

const today = new Intl.DateTimeFormat('fr-FR', {year: 'numeric', month: '2-digit', day: '2-digit'}).format(Date.now()).split('/').reverse().join('-');

How can I adapt it to get yesterday's date in yyyy-mm-dd format? Or is there a better way maybe?

🌐
Latenode
community.latenode.com › other questions › javascript
Convert date in JavaScript to yyyy-mm-dd format - JavaScript - Latenode Official Community
December 3, 2024 - I possess a date string in the form Sun May 11, 2014. What is the best way to transform it into the format 2014-05-11 using JavaScript? function formatDate(inputDate) { const dateObj = new Date(inputDate); const year = dateObj.getFullYear(); const month = String(dateObj.getMonth() + 1).padStart(2, '0'); const day = String(dateObj.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; } const date = 'Sun May 11, 2014'; console.log(formatDate(date)); The code snippet...
Find elsewhere
🌐
CoreUI
coreui.io › answers › how-to-format-date-as-yyyy-mm-dd-in-javascript
How to format date as YYYY-MM-DD in JavaScript · CoreUI
September 30, 2025 - For local timezone dates, use manual formatting with getFullYear(), getMonth() + 1, and getDate() instead. ... Follow Łukasz Holeczek on GitHub Connect with Łukasz Holeczek on LinkedIn Follow Łukasz Holeczek on X (Twitter) Łukasz Holeczek, ...
🌐
freeCodeCamp
freecodecamp.org › news › javascript-date-now-how-to-get-the-current-date-in-javascript
JavaScript Date Now – How to Get the Current Date in JavaScript
June 15, 2020 - const formatMap = { mm: date.getMonth() + 1, dd: date.getDate(), yy: date.getFullYear().toString().slice(-2), yyyy: date.getFullYear() };
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-format-javascript-date-as-yyyy-mm-dd
How To Format JavaScript Date as yyyy-mm-dd? - GeeksforGeeks
June 24, 2025 - Example: In this example, we will ... as yyyy-mm-dd ... const formatDateISO = (date) => { return date.toLocaleDateString('en-CA'); }; const currentDate = new Date(); console.log(formatDateISO(currentDate));...
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › date-tostring-format-yyyy-mm-dd
Format a JavaScript Date to YYYY MM DD - Mastering JS
December 10, 2021 - const date = new Date(); const year = date.getFullYear() * 1e4; // 1e4 gives us the the other digits to be filled later, so 20210000. const month = (date.getMonth() + 1) * 100; // months are numbered 0-11 in JavaScript, * 100 to move two digits ...
🌐
Medium
medium.com › @Saf_Bes › get-current-date-in-format-yyyy-mm-dd-with-javascript-9c898d1d5b22
Get current date in format yyyy-mm-dd with Javascript | by Saf Bes | Medium
April 10, 2020 - Get current date in format yyyy-mm-dd with Javascript · To get the current date in javascript, I use Intl.DateTimeFormat · new Intl.DateTimeFormat("fr-CA", {year: "numeric", month: "2-digit", day: "2-digit"}).format(Date.now()) the expected output : 2019-11-20 ·
🌐
Futurestud.io
futurestud.io › tutorials › how-to-format-a-date-yyyy-mm-dd-in-javascript-or-node-js
How to Format a Date YYYY-MM-DD in JavaScript or Node.js
/** * Returns the `date` formatted in YYYY-MM-DD. * * @param {Date} date * * @returns {String} */ function format (date) { if (!(date instanceof Date)) { throw new Error('Invalid "date" argument. You must pass a date instance') } const year = date.getFullYear() const month = String(date.getMonth() + 1).padStart(2, '0') const day = String(date.getDate()).padStart(2, '0') return `${year}-${month}-${day}` }
🌐
RSWP Themes
rswpthemes.com › home › javascript tutorial › how to get current date in yyyy-mm-dd format using javascript
How to Get Current Date in yyyy-mm-dd Format Using JavaScript
December 3, 2023 - In this article, we explored how to obtain the current date in the yyyy-mm-dd format using JavaScript. By leveraging the Date object’s toISOString() method and simple string manipulation, we can easily extract the desired format. Remember to create a new Date object and apply the necessary methods to achieve the desired result.
🌐
PTC Community
community.ptc.com › t5 › ThingWorx-Developers › Get-Date-yyyy-MM-dd-in-DateTime-format › td-p › 722959
Solved: Get Date (yyyy-MM-dd) in DateTime format - PTC Community
April 13, 2021 - // parseDate(stringDate:STRING, dateFormat:STRING):DATETIME var dateValue = parseDate(formattedDate, "YYYY-MM-DD");
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › parse
Date.parse() - JavaScript | MDN
// Standard date-time string format const unixTimeZero = Date.parse("1970-01-01T00:00:00Z"); // Non-standard format resembling toUTCString() const javaScriptRelease = Date.parse("04 Dec 1995 00:12:00 GMT"); console.log(unixTimeZero); // Expected output: 0 console.log(javaScriptRelease); // Expected output: 818035920000
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-format-javascript-date-as-yyyy-mm-dd.php
How to Format JavaScript Date as YYYY-MM-DD
// Create a date object from a ...ng("default", { day: "2-digit" }); // Generate yyyy-mm-dd date string var formattedDate = year + "-" + month + "-" + day; console.log(formattedDate); // Prints: 2022-05-04 · Here are some more ...
🌐
Articulate Community
community.articulate.com › articulate - community › connect › discuss articulate products
Using Javascript for X Days Ago in MM/DD/YYYY Format | Articulate - Community
October 4, 2023 - I need to have the learner open up this learning and have the current date displayed, as well as some dates prior to it (-10 days, -15 days, -2 months) in MM/DD/YYYY format. ... let month = currentTime.getMonth() + 1; let day = currentTime.getDate(); let year = currentTime.getFullYear();