Sunday, March 9, 2014

Adding a time and date stamp to the contacts database project

Just a small update to my contacts database app - I want to add a time and date stamp to each new entry that I add to the spreadsheet. This is achieved easily using the Javascript "new Date()" object, formatting it to look the way I want using "Utilities.formatDate" and then adding into my new row in the "apendRow" method.

The code to achieve this is as follows:

var d = Utilities.formatDate(new Date(),"EST","EEE', 'MMM dd', 'yyyy' at 'HH:mm:ss");
EST stands for Eastern Standard Time (I'm based on E coast of the US)
EEE gives the abbreviated 3 letter day of the week (e.g. Mon)
MMM gives the abbreviated 3 letter month of the year (e.g. Mar)
Then I modify my appendRow method to include the date variable "d" in the output. The code is as follows:

//apend row
sheet.appendRow([lastCell+1,d,textBoxValue1,textBoxValue2,textBoxValue3]);

So my contacts database now automatically adds a date/time stamp to each new entry I add, and looks like this:


References for this entry:
Stack Overflow on Javascript Date object
Stack Overflow on formatting Date object
Official Google documentation on formatDate

No comments:

Post a Comment