JavaScript Date
samples
These samples show how to use the JavaScript Date object.
Write the current date and time
The following sample gets the current date and time and then writes those values to two cells in the active worksheet.
function main(workbook: ExcelScript.Workbook) {
// Get the cells at A1 and B1.
let dateRange = workbook.getActiveWorksheet().getRange("A1");
let timeRange = workbook.getActiveWorksheet().getRange("B1");
// Get the current date and time with the JavaScript Date object.
let date = new Date(Date.now());
// Add the date string to A1.
dateRange.setValue(date.toLocaleDateString());
// Add the time string to B1.
timeRange.setValue(date.toLocaleTimeString());
}
Read an Excel date
This sample reads a date that's stored in Excel and translates it to a JavaScript Date
object. It uses the date's numeric serial number as input for the JavaScript Date
. This serial number is described in the NOW() function article.
function main(workbook: ExcelScript.Workbook) {
// Read a date at cell A1 from Excel.
let dateRange = workbook.getActiveWorksheet().getRange("A1");
// Convert the Excel date to a JavaScript Date object.
let excelDateValue = dateRange.getValue() as number;
let javaScriptDate = new Date(Math.round((excelDateValue - 25569) * 86400 * 1000));
console.log(javaScriptDate);
}
See also
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
Office Scripts