Help

Script to put current Date/Time into a Date/Time field with a button

Topic Labels: Scripting extentions
Solved
Jump to Solution
9772 3
cancel
Showing results for 
Search instead for 
Did you mean: 
Josh_Cooper
6 - Interface Innovator
6 - Interface Innovator

So this is probably a dumb question, but I have hit a wall! How do you write into a Date/Time field with the current date in the locale timezone? I have tried the .toLocaleString() method and the console will output everything perfectly, but nothing shows up in the field.

This is what I have.

var rightNow = new Date();
var res = rightNow.toLocaleString();
table.updateRecordAsync(record,{
    "Date/Time Submitted To Shipping": res
})

Any help would be greatly appreciated!

Update:

I can get the Date/Time in a Date/Time field by using the .toISOString() method, but the time is 5 hours off.

1 Solution

Accepted Solutions
Justin_Barrett
18 - Pluto
18 - Pluto

There’s no need to convert the date. Just put it directly into the date field.

A couple other tips:

  • Use let instead of var
  • Add “await” before the update so that the update fully completes before the next part of your code, or else you might get unpredictable results.
let rightNow = new Date();
await table.updateRecordAsync(record,{
    "Date/Time Submitted To Shipping": rightNow
});

You could also create the date on the fly:

await table.updateRecordAsync(record,{
    "Date/Time Submitted To Shipping": new Date()
});

I used the latter option in a test just now, and it worked fine.

See Solution in Thread

3 Replies 3
Justin_Barrett
18 - Pluto
18 - Pluto

There’s no need to convert the date. Just put it directly into the date field.

A couple other tips:

  • Use let instead of var
  • Add “await” before the update so that the update fully completes before the next part of your code, or else you might get unpredictable results.
let rightNow = new Date();
await table.updateRecordAsync(record,{
    "Date/Time Submitted To Shipping": rightNow
});

You could also create the date on the fly:

await table.updateRecordAsync(record,{
    "Date/Time Submitted To Shipping": new Date()
});

I used the latter option in a test just now, and it worked fine.

Josh_Cooper
6 - Interface Innovator
6 - Interface Innovator

Thanks! That definitely simplifies things, but it still returns the ISO standard date/time, not the local date/time. How do I get the current local date/time?

I tried to reconstruct the ISO date/time using the getTimeZoneOffset() method as follows.

Date.prototype.toISOString = function() {
    var tzo = -this.getTimezoneOffset(),
        dif = tzo >= 0 ? '+' : '-',
        pad = function(num) {
            var norm = Math.floor(Math.abs(num));
            return (norm < 10 ? '0' : '') + norm;
        };
    return this.getFullYear() +
        '-' + pad(this.getMonth() + 1) +
        '-' + pad(this.getDate()) +
        'T' + pad(this.getHours()) +
        ':' + pad(this.getMinutes()) +
        ':' + pad(this.getSeconds()) +
        dif + pad(tzo / 60) +
        ':' + pad(tzo % 60);
}

var dt = new Date();
console.log(dt.toISOString().slice(0,19));

await table.updateRecordAsync(record,{
    "Date/Time Submitted To Shipping":dt.toISOString().slice(0,19)
});

Believe it or not the console logs the correct local time in the ISO format, but the date/time field in the table has the GMT time??

My wife just asked “Is it a setting or something?” Keep in my mind she thinks Airtable is something you eat on in the sky. Guess what? I had the GMT setting off in the field settings!!

Problem solved!

Thanks for the help Justin!!