user3511512
user3511512

Reputation: 115

Google Apps Script Time from Datetime number

I have a Google sheet with datetime numbers in the following format: 42,385.744166666700

I am writing a script and I want to extract the time from these numbers in the format HH:MM. The above number should return 17:51.

I tried this code but does not work:

function extractMicrosTime(DateTimeNumber) {
  var DateNumber = new Date(DateTimeNumber);
  var hours = DateNumber.getHours();
  var minutes = DateNumber.getMinutes();

  return hours + ":" + minutes;

}

Upvotes: 0

Views: 2662

Answers (2)

Serge insas
Serge insas

Reputation: 46794

For info, this can be done with script as well. Example below for column A

function formatColumn() {
  var sh = SpreadsheetApp.getActiveSheet();
  var col = sh.getRange('A1:A');
  col.setNumberFormat('dd/MM/yyyy HH:mm:ss');
}

Upvotes: 1

Tom Woodward
Tom Woodward

Reputation: 1713

Your function will work if you format the source cell as Date time.

format date time

If you do use it raw it returns 19:0

A working example is here.

Upvotes: 0

Related Questions