Date Hell

Hello @Mark
Is it possible when using the “date-time” type that glide does a conversion from the regional format to the “us” format automatically (or at the limit to the ISO format, because I think that is to accept by new Date(…)).

Because my browser is in French, so the format is in dd / mm / yyyy except that if I send a Date column directly to YC (for example 15/8/2021) and I do a
Let x = new Date (date.value) I get an invalid date error, logical since js is in “us” format.

And if possible to have the same for the Array, because even worse if I make a date array (Split) from a Date column, the data is in “string” format and I have to take into account the format ( “Short, medium, long”) to be able to make a conversion.
I had to do a lot of code (and adjust the driver in the process) to try to convert the regional format to a usable date in JS and the reverse too

Unless I’m wrong, but the YC will run on the client side, so with the user’s regional format.

1 Like

Yep, the date as a parameter for YC works really bad. I’ve also spent a lot of time doing custom code with it.

I’ve ended with sending date as a “string” and not “date-time”. And JavaScript parses date from that string in a predictable manner.

1 Like

Yes, in the little that I’ve played around with it, this is what I’ve been doing.

Yes the problem is who has to see it internationally, and it’s more the same story as making a simple conversion for your own format.

Right! It’s a tragedy!

I have struggled with the same formatting issue since last week and working with arrays the code becomes more complex and larger unnecessarily.

Every datetime value has to be parsed to check if it’s a valid date (dd/mm/yyyy), if not, we have to split the date, take each day, month and year, move them and create a new valid format (mm/dd/yyyy or yyyy/mm/dd) to get a correct value to compare or calculate values based on dates.

Every time I found a Date array and I need to analyze it, the date format issue drives me crazy unnecessarily.

I can see now I’m not the only one crazy and with headaches due to it :woozy_face:

Saludos a todos!

2 Likes

I share with you the code I made to test the Glide <-> JS conversion.
There are 2 two scripts:

  1. for a Date column
  2. for an Array Split (depends on the 1st script)

The idea is to come and go and confirm the conversion.

Script 1, Date Column Test YC
https://Date-Test.manun2.repl.co
Source
https://replit.com/@ManuN2/Date-Test?v=1

Script 2, Array Test YC
https://Date-Test-Array.manun2.repl.co
Source :
https://replit.com/@ManuN2/Date-Test-Array?v=1

Please give me a feedback, and if you encounter a bug (it’s likely)

Hola @Manu.n

it’s my code so far to sort and return a Date array (ascending) when a user has the date format dd/mm/yyyy .

It sorts the date and time at the same time and needs a date array of course to work.
In my case, I use a Hell Yes-Code version (from Mark’s version) where p8 parameter is the original (raw) Date Array:

p8.sort(function (a, b) {
  //  '13/9/2021 12:00:20'.split(' ') =>  ["13/9/2021", "12:00:20"]
  let DTa = a.split(' ');  DTb = b.split(' ');

  // '13/9/2021'.split('/') =>  ["13", "9", "2021"]
  let da = DTa[0].split('/');
  let db = DTb[0].split('/');

  let HMSa=DTa[1].split(':');
  let HMSb=DTb[1].split(':');

  //  Hour
  let ha = HMSa[0]; 
  let hb = HMSb[0];
 
  //  Minute
  let ma = HMSa[1]; 
  let mb = HMSb[1];

  //  Secs
  let sa = HMSa[2]; 
  let sb = HMSb[2];

  return     da[2] - db[2] || da[1] - db[1] ||  
             da[0] - db[0] || ha - hb       || 
              ma - mb      || sa - sb 
});
 
 // return the new sorted array
 return p8

My replit URL is:
https://Array-YesCode.gustavovalero.repl.co

Saludos!

1 Like

Hi,
Nice,
But how do you determine if this or that user is in dd / mm / yyyy or mm / dd / yyyy format

Not related to YC, but I recently had to deal with a similar challenge.

I had to write some code to import data from an external source, and the date formatting was all over the place. Specific issues:

  • Some dates were formatted as dd/mm/yyyy
  • Some dates were formatted as mm/dd/yyyy
  • Some dates were formatted as either of the above, but with trailing spaces which turned them into strings, which meant a new Date(date) call would fail.

The only thing I knew for certain was that all dates were supposed to be for the same month/year, and I knew which month/year that should be. So armed with that knowledge, I wrote the following function to validate and standardise them:

function check_valid_date(date, expected_month) {
  var expected_month_index = LONG_MONTH_NAMES.indexOf(expected_month);
  var data_type = typeof date;

  switch (data_type) {
    case "object": // It's probably a date
      var checkdate = new Date(date);
      if (checkdate.getMonth() === expected_month_index) {
        // It's a valid date for the correct month, yay!
        return checkdate;
      }
      break;
    case "string":
      date = date.trim();
      var checkdate = new Date(date);
      if (checkdate.getMonth() === expected_month_index) {
        // It's a valid date for the correct month, yay!
        return checkdate;
      }
      else {
        // Check the delimiters, then swap the day/month bits
        var delimiter = date.includes('/') ? '/' : '-';
        var parts = date.split(delimiter);
        checkdate = new Date(Number(parts[2]), Number(parts[1]) - 1, Number(parts[0]));
        if (checkdate.getMonth() === expected_month_index) {
          // It's a valid date for the correct month, yay!
          return checkdate;
        }
      }
      break;
    default:
      return undefined;
  }
}

I need to parse and import this data on a daily basis, and so far the above seems to be working well (touch wood) :pray:

3 Likes

Epale @Manu.n

Do you think it’s possible to make a code that changes from 1 to a specific number based on the NOW() (date of form submission) the back to 1 when it gets to the max number?

Hello @SantiagoPerez
Could you be a little more specific. I have a little trouble following you.
The number should automatically change based on now?

@Manu.n

Yes, the number should change based on Now.

Mínimum will always be 1 and Max is going to be dynamic. The YC gives a number from 1 to Max as Now changes. When it gets to the max number, it goes back to 1 and starts all over again.

Ok and the number is incremented according to the seconds? minutes ? time ? day ? … ???

I’d say seconds.

Here is a small script that increments a number back based on the elapsed seconds.
Warning: Glide only updates every 10 seconds or more. So the precision is 10 seconds.

YC Link
https://Now-Inc.manun2.repl.co

YC Source

1 Like

Thanks @Manu.n

Look at this: https://vimeo.com/602662401/47e5947cdd

1 Like

Hola @SantiagoPerez ,

I think you are looking for something like this:

The Carousel is ruled to change every 10 sec (Glide’s Update) so, the logo in your screen will change every 10 secs.

image image

The Hell Yes Code is this:

let Counter=parseInt(p1/10);

switch(Counter) {
  case 0:
    return p8[Counter]
    break;
  case 1:
    return p8[Counter]
    break;
  case 2:
    return p8[Counter]
    break;
  case 3:
    return p8[Counter]
    break;
  case 4:
    return p8[Counter]
    break;
  case 5:
    return p8[Counter]
 }

image

My YC link to test is: https://SNB-YesCode.gustavovalero.repl.co

Saludos y espero te sirva!

2 Likes

Santiago!!
watching the @Robert_Petitto’s trick, you might do it without using an YC code:

It’s a very easy way as well.

Saludos

2 Likes

Hi
I am trying to use this code to replace date format from mm\dd\yyy to dd\mm\yyyy.
I am using your user system YC to determine what language the browser is and changing accordingly.
https://Agent.manun2.repl.co
image

I tried this first: https://Date-Test.manun2.repl.co but the column stays empty.

then I tried splitting to array: https://Date-Test-Array.manun2.repl.co but getting an error.

What am I doing wrong and how can I fix it?

Hello,
For the test date it should work !! I check if it still works

For the date array, it is normal the array is not good.
See the format on the image.
(it also works in short format)