Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7862173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:01:31+00:00 2026-06-02T23:01:31+00:00

I really thought I was starting to understand how this works until I tried

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:01:32+00:00Added an answer on June 2, 2026 at 11:01 pm

    There does seem to be something strange going on with the behavior of your =dateToSeconds() versus your test() function. It looks like there is a bug that’s causing the time discrepancy between the two. Please raise a bug for that in the issue tracker.

    I was however able to get this working by using number formatting on the cells. Have a look at my modified copy of your spreadsheet. In cell A2 I entered “29/04/2012 11:00:00” and then set Format > Number > 15:59:00 Time. You can also do this programmatically by using Range.setNumberFormat(“HH:mm:ss”); This results in cell A2 showing 11:00:00.

    Then, I modified your test function to have this code:

    function test() {
      var sheet = SpreadsheetApp.getActiveSheet();
    
      sheet.getRange("E2").setValue(dateToSeconds(sheet.getRange("A2").getValue()));
      Logger.log(sheet.getRange("A2").getValue());
      Logger.log(sheet.getRange("A2").getNumberFormat());
    
      var values = sheet.getRange("A4:C4").getValues();
      Logger.log(values[0][0] + " " + values[0][1] + " " + values[0][2]);
      sheet.getRange("E4").setValue(newTime2(values[0][0], values[0][1], values[0][2])).setNumberFormat("HH:mm:ss");
    }
    

    and instead of a newTime function, I created a newTime2 with this code:

    function newTime2(hours, minutes, seconds) {
      var date = new Date();
      date.setHours(hours);
      date.setMinutes(minutes);
      date.setSeconds(seconds);
      Logger.log(date);
      return date;
    }
    

    I hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really thought I understood Python variable referencing, so I'm confused why this code
This post asks this question but doesn't really give an answer, so I thought
I thought that I was really starting to understand how pointers work, and then
I've got this really simple piece of code that I thought was the correct
I'm not really familiar with creating generic methods, so I thought I'd put this
Before starting writing this question, i was trying to solve following // 1. navigate
this is just kind of a broad question to get some starting points. I
I don't really think the iOS part of this question really matters too much,
I thought this would be really simple but its proving to be very difficult.
I'm trying to render to a texture (really thought it would be easier than

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.