[GIS] How to get current date “YYYY-MM-DD” in string format in GEE script

dategoogle-earth-enginejavascriptstring

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:

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.

Related Question