0
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataa = ss.getSheetByName("123").getRange("C1").getValue();
// C1 => 18:34:00.000
Logger.log(Utilities.formatDate(dataa, "GMT", "HH:mm"));
//Log =>[20-01-24 17:31:02:475 JST] 10:06

I bring the date of the spread sheet.

It was expressed as a google app script date.

However, the results are different.

Why is this like this?

enter image description here

10
  • 1
    what are you trying to do with the dates? Commented Jan 24, 2020 at 8:52
  • 1
    The date is from 1 timezone and you're formatting it to another it seems. Check your spreadsheet & script timezones. Commented Jan 24, 2020 at 8:55
  • 1
    You might not need to format for comparison purposes. Also as ross said, the timezone of your spreadsheet is most likely different from GMT Commented Jan 24, 2020 at 9:00
  • 2
    @cloudbabby Please show us what the value in C1 looks like in the spreadsheet. That output looks like duration, not datetime. Commented Jan 24, 2020 at 9:01
  • 1
    If the format is always the same hh:mm:ss then you can split them as a string on : and compare the resulting array values: [hh,mm,ss]. Also convert them to numbers first using array.map Commented Jan 24, 2020 at 9:19

1 Answer 1

2

The problem you're facing is with .getValue(), which will parse the data in the cell into whatever Google thinks it is. To get the value exactly as you see it in the spreadsheet, use .getDisplayValue().

Assuming that the values in column C are always in the same format (HH:mm:ss), you can simply split the value using the colon as delimiter and then compare these values against your control. (Take a look at the various Date methods to compare against hours, minutes, etc.)

var range = ss.getSheetByName("Sheet1").getRange("C1"); // 18:34:00
var displayValue = range.getDisplayValue();
var displayValueParts = displayValue.split(":");
var hour = displayValueParts[0];
var minutes = displayValueParts[1];
var seconds = displayValueParts[2]

Note here that just using .getValue(), Google interprets the value in C1 as a date.

var range = ss.getSheetByName("Sheet1").getRange("C1"); // 18:34:00
var value = range.getValue();
Logger.log(value); // Sat Dec 30 11:51:56 GMT+00:00 1899
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.