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 OverflowJust 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]
You can do:
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('-');
}
console.log(formatDate('Sun May 11,2014'));
Usage example:
console.log(formatDate('Sun May 11,2014'));
Output:
2014-05-11
Demo on JSFiddle: http://jsfiddle.net/abdulrauf6182012/2Frm3/
javascript - How to get current date "YYYY-MM-DD" in string format in GEE script - Geographic Information Systems Stack Exchange
How to get yesterday's date in yyyy-mm-dd format?
Convert date in JavaScript to yyyy-mm-dd format
How get a date formatted like 2023-02-07 in JS?
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.
This is the simplest option I can come up with:
new Date().toISOString().split('T')[0]
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?