BUG: result as 'date-time' is not returned

I have a function that should return Date Time for further sort. It never returns the value. I’ve dug and cannot find anything. In addition, the documentation doesn’t have examples where the result will be ‘date-time’.
But even with a dead simple function - the result is blank:

window.function = function () {
  return new Date()
}

glide.json:

    "result": {
      "type": "date-time"
    }

How it looks in GDE:

Any thoughts @Manu.n @gvalero? I don’t use date-time that much in Replit. Seeing as you guys have worked with this a lot this week hopefully you have an answer.

1 Like

Hola @Maxim_KM

Are you using a Hell Yes-Code script right?

Try this directly:

return New Date()

Or this one:

return New Date().toLocaleFormat()

Let me now what you get!

Bye

1 Like

Hello @Maxim_KM
You may not have the correct version of the driver.js
Take a test with this one:


function convert(x) {
  if (x instanceof Date) {
    return x.toISOString();
  } else if (Array.isArray(x)) {
    return x.map(convert);
  } else {
    return x;
  }
}

window.addEventListener("message", async function(event) {
  const { origin, data: { key, params } } = event;
 
  let result;
  let error;
  try {
    result = await window.function(...params);
  } catch (e) {
    result = undefined;
    try {
      error = e.toString();
    } catch (e) {
      error = "Exception can't be stringified.";
    }
  }

  const response = { key };
  if (result !== undefined) {
    result = convert(result);
    response.result = { type: "string", value: result };
  }
  if (error !== undefined) {
    response.error = error;
  }

  event.source.postMessage(response, "*");
});

If you make a new Date with this version of the driver, you will have the date in GMT.

you have to return the date in string format, so yes @gvalero with toLocaleDateString () should also work.
(note that toLocaleFormat is deprecated
Deprecated and obsolete features - JavaScript | MDN)

But if you want to manipulate dates, I encourage you to take this ‘date-Test’ script as a starting point, you don’t have to worry about regional settings or the time difference for the return date-times. .

if you have any questions, i’m getting to know the subject a bit!

2 Likes

Yep, driver.js with convert function solves problem. Hadn’t realized that can debug and fix it’s work :slight_smile:
@Mark I propose updating example scripts with better driver.js, as many of us use them as starting point for own functions(including dates)

Thanks for the advice @Manu.n, I haven’t read this warning before!

Feliz día

Hi again,

Just to document to problem source:

If this statement is omitted or deleted in Driver.js, the New Date() sentence will return blank always:
return x.toISOString();

image

image

To get a valid value (date), you must use this method or other equivalent:

new Date().toLocaleString()

image

Instead, if you keep return x.toISOString() in your in Driver.js, the Date() command will return a value (what Maxim_KM can’t get):

image

image

That was one of rocks I faced days ago trying to understand how date formats work and return values

Feliz día!

2 Likes

Thanks for this detailed post!