Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Set Number and Date Formats

    • 4 minutes to read

    A number format specifies the display format of a cell value in a worksheet. Number formats can control the display of numeric values, dates, and times. Use the CellFormat.NumberFormat property to apply a number format to a cell or cell range.

    Specify a Number Format

    Assign a NumberFormat object to the CellFormat.NumberFormat property.

    The following code snippet displays a number with two decimal places:

    Cell cell = worksheet.Cells["D1"];
    cell.Value = 1234.56;
    cell.Format.NumberFormat = new NumberFormat("#,##0.00");
    

    Use NumberFormat.General to apply the default General format:

    cell.Format.NumberFormat = NumberFormat.General;
    

    Common Number Formats

    Format Code Description Example
    0 Displays an integer. 1234
    0.00 Displays two decimal places. 1234.50
    #,##0 Displays an integer with a thousands separator. 1,234
    #,##0.00 Displays a number with a thousands separator and two decimal places. 1,234.50
    0.00% Displays a value as a percentage with two decimal places. 25.00%
    $#,##0.00 Displays a number as a dollar amount with two decimal places. $1,234.50

    Specify Date and Time Formats

    The Spreadsheet Document API stores dates and times as cell values and uses number formats to control their display.

    Use the Cell.SetDateValue(DateTime, NumberFormat) method to assign a date or time value to a cell and specify the display format.

    The following code snippet displays a date in the month/day/year format:

    Cell cell = worksheet.Cells["D1"];
    cell.SetDateValue(
        new DateTime(2025, 12, 17),
        new NumberFormat("m/d/yyyy"));
    

    Use the following format codes to display dates in different formats:

    Format Code Description Example
    m/d/yyyy Month, day, and four-digit year. 12/17/2012
    d-mmm-yyyy Day, abbreviated month, and four-digit year. 17-Dec-2012
    d-mmm-yy Day, abbreviated month, and two-digit year. 17-Dec-12
    d-mmm Day and abbreviated month. 17-Dec
    mmm-yyyy Abbreviated month and four-digit year. Dec-2012
    mmmm d, yyyy Full month name, day, and four-digit year. December 17, 2012
    dddd, mmmm d, yyyy Full day name, full month name, day, and four-digit year. Monday, December 17, 2012

    The following code snippet displays a date with the full month name:

    Cell cell = worksheet.Cells["D1"];
    cell.SetDateValue(
        new DateTime(2025, 12, 17),
        new NumberFormat("mmmm d, yyyy"));
    

    Time Formats

    Use the following format codes to display time values:

    Format Code Description Example
    h:mm Hours and minutes. 14:30
    h:mm:ss Hours, minutes, and seconds. 14:30:45
    h:mm AM/PM Hours and minutes with an AM/PM indicator. 2:30 PM
    h:mm:ss AM/PM Hours, minutes, and seconds with an AM/PM indicator. 2:30:45 PM

    The following code snippet displays time in hours, minutes, and seconds:

    Cell cell = worksheet.Cells["D1"];
    cell.SetDateValue(
        new DateTime(2025, 7, 10, 14, 30, 45),
        new NumberFormat("h:mm:ss"));
    

    Combined Date and Time Formats

    Combine date and time format codes to display both parts of a DateTime value.

    Format Code Description Example
    m/d/yyyy h:mm Date, hours, and minutes. 12/17/2012 14:30
    m/d/yyyy h:mm:ss Date, hours, minutes, and seconds. 12/17/2012 14:30:45
    m/d/yyyy h:mm AM/PM Date, hours, and minutes with an AM/PM indicator. 12/17/2012 2:30 PM
    m/d/yyyy h:mm:ss AM/PM Date, hours, minutes, and seconds with an AM/PM indicator. 12/17/2012 2:30:45 PM

    The following code snippet displays a date and time with seconds:

    Cell cell = worksheet.Cells["D1"];
    cell.SetDateValue(
        new DateTime(2025, 7, 10, 14, 30, 45),
        new NumberFormat("m/d/yyyy h:mm:ss"));
    

    Create Custom Format Codes

    You can combine format code symbols to create custom number, date, and time formats.

    The following symbols are commonly used in custom formats:

    Symbol Description
    0 Displays a digit and 0 when no digit exists in this position.
    # Displays a digit only when a digit exists at that position.
    . Specifies the decimal separator.
    , Specifies a thousands separator when placed between digit placeholders.
    % Displays a value as a percentage and adds the percent symbol.
    @ Displays the text value in a cell.
    d Displays the day.
    m Displays the month.
    y Displays the year.
    h Displays the hour.
    s Displays seconds.
    AM/PM Displays an AM or PM indicator.

    The following code snippet displays a date and time in the day-month-year hour:minute format:

    Cell cell = worksheet.Cells["D1"];
    cell.SetDateValue(
        new DateTime(2025, 7, 10, 14, 30, 45),
        new NumberFormat("d-MMM-yyyy h:mm"));
    

    You can combine numeric format symbols with date and time symbols as needed.

    Next Steps

    See Also