Google-sheets – Script gives 1899 for the timestamp year. How to fix it

google sheetsgoogle-apps-scriptgoogle-sheets-timestamp

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);
        };
    };
}

Best Answer

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