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 parameter for "value:" of ui.Textbox()
function.
Could you please help me ?
Code of verification of mine is follows.
var now = Date.now(); //getting time at UTC
var eeNow = ee.Date(now); //converting number to object format
print("now:",typeof(now),now);
print("eeNow:",typeof(eeNow),eeNow);
var y=eeNow.get('year');
var m=eeNow.get('month');
var d=eeNow.get('day');
print("year:",typeof(y),y);
print("month:",typeof(m),m);
print("day:",typeof(d),d);
var Today01=ee.Date.fromYMD(
eeNow.get('year'),
eeNow.get('month'),
eeNow.get('day'));
// eeNow.get('day')).toString();
print("Today01:",typeof(Today01),Today01);
var Today02 = y.toString()+'-'+m.toString()+'-'+d.toString();
print("Today02:",typeof(Today02),Today02);
//
// Final aim : to set "value:"parameter with current "YYYY-MM-DD" in String format
//
//var TxBoxt = ui.Textbox({
////////////////////////value: '2020-03-16',
// value :Today01,
// style: {width : '90px'},
// onChange: function(text) {
// var period_former_end = text;
// }
//});
Best Answer
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:
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:
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/PMOption 3
Do the formatting yourself. This is "imported" from this SO answer:
Advantage is that you can code pretty much any format you want.
Disadvantage is that you have to reinvent the wheel.