Take the 2-minute tour ×
Web Applications Stack Exchange is a question and answer site for power users of web applications. It's 100% free, no registration required.

The following code was almost exactly what I was looking for to timestamp the adjacent cell after being edited. However, I need to incorporate the currnet date as well as time. The code below gives a date "1899".

What do I need to edit or is there a better code?

function onEdit() {
    var s = SpreadsheetApp.getActiveSheet();
    if ( s.getName() == "Sheet1" ) { //checks that we're on the correct sheet
        var r = s.getActiveCell();
        if ( r.getColumn() == 13 ) { //checks the column
            var nextCell = r.offset(0, 1);
            if ( nextCell.getValue() === '' ) //is empty?
                var time = new Date();
                time = Utilities.formatDate(time, "GMT", "HH:mm:ss");
                nextCell.setValue(time);
        };
    };
}
share|improve this question

1 Answer 1

If you need to display date along with the timestamp, then look at this line:

time = Utilities.formatDate(time, "GMT", "HH:mm:ss");

You can set this to whatever you like, in the year case try this:

time = Utilities.formatDate(time, "GMT-08:00", "MM/DD/yy, hh:mm:ss");

If you are only concerned about displaying the year, then replace the MM/DD/yy info with just yyyy

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.