google-apps-scriptgoogle-sheetstimetimestamp

Why is my timestamp's milliseconds the same as it's seconds?


I am using the following formula:

function TIMESTAMP() {
  return Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'HH:mm:ss:SSS" - "dd/MM');
}


function onEdit(e) {
  const excludeSheetNames = ["START", "MASTER"]; // Please set exclude sheet names.

  const { range } = e;
  const sheet = range.getSheet();
  //if (excludeSheetNames.includes(sheet.getSheetName()) || range.columnStart != 4 || range.rowStart == 1) return;
  if (excludeSheetNames.includes(sheet.getSheetName()) || ![2, 4].includes(range.columnStart) || range.rowStart == 1) return;

  range.offset(0, 1).setNumberFormat('HH:mm:ss:SSS" - "dd/MM').setValue(new Date());

  if (e.value != "TRUE") return;
  e.source.getActiveSheet().getRange(e.range.rowStart, e.range.columnStart + 1).setValue(new Date()).setNumberFormat('HH:MM:SS - dd/MM ');
}

The result is always like the attached picture:
Timestamp

The milliseconds return the same number like the seconds. Why?

15:34:33:033 - 25/10
15:34:35:035 - 25/10
15:34:36:036 - 25/10
15:34:37:037 - 25/10
15:34:39:039 - 25/10

16:21:24:024 - 25/10
16:21:25:025 - 25/10
16:21:28:028 - 25/10
16:21:29:029 - 25/10
16:21:30:030 - 25/10

Solution

  • The number format for milliseconds is incorrect.

    As written in docs,

    0 - Tenths of seconds. You can increase the precision to two digits with 00 or three digits (milliseconds) with 000.

    ss - Seconds in the minute with a leading 0.

    Replace

    HH:mm:ss:SSS - dd/MM
    

    with

    HH:mm:ss.000 - dd/MM