Dates & Times


This script gets the current date and time and then writes those values to two cells in the active worksheet.

function main(workbook: ExcelScript.Workbook) { 
  let date = new Date(Date.now());
  let dateRange = workbook.getActiveWorksheet().getRange("A1");
  dateRange.setValue(date.toLocaleDateString());
  let timeRange = workbook.getActiveWorksheet().getRange("B1");
  timeRange.setValue(date.toLocaleTimeString());
}

This script reads a date from cell "A1" and translates it to a JavaScript Date object.
It uses the date's numeric serial number as input for the JavaScript Date.

function main(workbook: ExcelScript.Workbook) { 
  let dateRange = workbook.getActiveWorksheet().getRange("A1");

  let excelDateValue = dateRange.getValue() as number;
  let javaScriptDate = new Date(Math.round((excelDateValue - 25569) * 86400 * 1000));
  console.log(javaScriptDate);
}


© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext