I really thought I was starting to understand how this works until I tried to distil a test case for this question and am totally confused again.
With a google spreadsheet you can write a custom function like this:
function dateToSeconds(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
return (hours*3600) + (minutes*60) + seconds;
}
and call it in a cell by doing e.g.
=dateToSeconds(A1)
and it does what you expect. However, if you call it like so:
function test() {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange("A2").setValue(dateToSeconds(sheet.getRange("A1").getValue()));
}
there's a discrepancy of 25m 21s. What the heck?
Also, if you want to return a date object from a script so that it is formatted as a time, you can do e.g.
function newTime(hours, minutes, seconds) {
// 1899-12-30 is the epoch for time values, it seems
// http://stackoverflow.com/questions/4051239/how-to-merge-date-and-time-as-a-datetime-in-google-apps-spreadsheet-script
return new Date(1899, 11, 30, hours, minutes, seconds, 0);
}
and call it by doing:
=newTime(A1, A2, A3)
and it works fine. If you do:
function test() {
var sheet = SpreadsheetApp.getActiveSheet();
var values = sheet.getRange("A1:C3").getValues();
sheet.getRange("D3").setValue(newTime(values[0], values[1], values[2]));
}
then (given an input of 11:00:00) it gets formatted as a date like:
01/01/1970 01:00:00
which looks like it's being interpreted as an hour after the more traditional 1970 epoch? I thought the behaviour I had seen before was a bit symmetrical with the 25m 21s offset above and I'd see:
10:34:39
My test case is here