ExcelScript.DatetimeFormatInfo interface

Определяет формат отображения чисел, соответствующий культуре. Это основано на текущих параметрах языка и региональных параметров системы.

Комментарии

Примеры

/**
 * This script sets the value of a cell to a date string for January 2, 2023.
 * It writes the day or month first in the string based on system settings.
 */
function main(workbook: ExcelScript.Workbook) {
  // Get the first cell in the current worksheet.
  const cell = workbook.getActiveWorksheet().getCell(0,0);

  // Get the date format.
  const cultureInfo : ExcelScript.CultureInfo = workbook.getApplication().getCultureInfo();
  const systemDateTimeFormat : ExcelScript.DatetimeFormatInfo = cultureInfo.getDatetimeFormat();
  const shortDatePattern : string = systemDateTimeFormat.getShortDatePattern();

  // Determine if the date should start with the month or day.
  if (shortDatePattern.startsWith("m")) {
    cell.setValue("1/2/2023");
  } else {
    cell.setValue("2/1/2023");
  }
}

Методы

getDateSeparator()

Возвращает строку, используемую в качестве разделителя даты. Это зависит от текущих параметров системы.

getLongDatePattern()

Возвращает строку формата для длинного значения даты. Это зависит от текущих параметров системы.

getLongTimePattern()

Возвращает строку формата для длительного значения времени. Это зависит от текущих параметров системы.

getShortDatePattern()

Возвращает строку формата для короткого значения даты. Это зависит от текущих параметров системы.

getTimeSeparator()

Возвращает строку, используемую в качестве разделителя времени. Это зависит от текущих параметров системы.

Сведения о методе

getDateSeparator()

Возвращает строку, используемую в качестве разделителя даты. Это зависит от текущих параметров системы.

getDateSeparator(): string;

Возвращаемое значение

string

Примеры

/**
 * This script writes the current date, month, and year.
 * It uses the system's date separator character.
 */
function main(workbook: ExcelScript.Workbook) {
  // Get the first cell in the current worksheet.
  const cell = workbook.getActiveWorksheet().getCell(0,0);

  // Get the date separation string.
  const cultureInfo = workbook.getApplication().getCultureInfo();
  const systemDateTimeFormat = cultureInfo.getDatetimeFormat();
  const separator = systemDateTimeFormat.getDateSeparator();

  // Get the current date.
  const currentDate = new Date(Date.now());

  // Write the date using the system's separator character.
  cell.setValue(`${currentDate.getMonth()}${separator}${currentDate.getDate()}${separator}${currentDate.getFullYear()}`);
}

getLongDatePattern()

Возвращает строку формата для длинного значения даты. Это зависит от текущих параметров системы.

getLongDatePattern(): string;

Возвращаемое значение

string

Примеры

/**
 * This script returns the system's long date pattern. 
 * This could be used in a Power Automate flow to keep date formatting consistent.
 */
function main(workbook: ExcelScript.Workbook) : string {
    const cultureInfo = workbook.getApplication().getCultureInfo();
    const dateTimeInfo =  cultureInfo.getDatetimeFormat();
    
    return dateTimeInfo.getLongDatePattern();
}

getLongTimePattern()

Возвращает строку формата для длительного значения времени. Это зависит от текущих параметров системы.

getLongTimePattern(): string;

Возвращаемое значение

string

getShortDatePattern()

Возвращает строку формата для короткого значения даты. Это зависит от текущих параметров системы.

getShortDatePattern(): string;

Возвращаемое значение

string

getTimeSeparator()

Возвращает строку, используемую в качестве разделителя времени. Это зависит от текущих параметров системы.

getTimeSeparator(): string;

Возвращаемое значение

string

Примеры

/**
 * This script writes the current hour, minute, and second.
 * It uses the system's time separator character.
 */
function main(workbook: ExcelScript.Workbook) {
  // Get the first cell in the current worksheet.
  const cell = workbook.getActiveWorksheet().getCell(0, 0);

  // Get the date separation string.
  const cultureInfo = workbook.getApplication().getCultureInfo();
  const systemDateTimeFormat = cultureInfo.getDatetimeFormat();
  const separator = systemDateTimeFormat.getTimeSeparator();

  // Get the current time.
  const currentTime = new Date(Date.now());

  // Write the date using the system's separator character.
  cell.setValue(`${currentTime.getHours()}${separator}${currentTime.getMinutes()}${separator}${currentTime.getSeconds()}`);
}