Date-time and number formats let you control how your data appears in a sheet. Google Sheets provides some common formats to choose from, but you can also define your own formats.
In the Sheets UI, you apply number and date formats to cells using the Format> Numbermenu. In the Sheets API, you set these formats using a spreadsheets.batchUpdate
method call to send an UpdateCellsRequest
or RepeatCellRequest
.
This page describes how to define new date and number format patterns, which you
can include in your API request. The sample Set a custom datetime or decimal format for a range
shows how to set a format pattern using the API. Note that the actual rendering
of the format depends on the spreadsheet's locale
. This guide assumes the locale
is en_US
. You can determine the locale
of a spreadsheet by reading
the SpreadsheetProperties
with a spreadsheets.get
request.
About date & time values
Sheets, like most other spreadsheet applications, treats date and time values as decimal values. This lets you perform arithmetic on them in formulas, so you can increment days or weeks, add or subtract two dates and times, and perform other similar operations.
Sheets uses a form of epoch date
that's commonly used in spreadsheets. The whole number portion of the value
(left of the decimal) counts the days since December 30, 1899. The fractional
portion (right of the decimal) counts the time as a fraction of one day. For
example, January 1, 1900 at noon is 2.5
, 2
because it's 2 days after
December 30, 1899, and 0.5
because noon is half a day. February 1, 1900 at 3 PM
is 33.625
.
Sheets correctly treats the year 1900 as a common year, not a leap year.
Date & time format patterns
A date-time format pattern is a string of token substrings that, when parsed, are replaced with the corresponding date-time elements (such as the month or hour).
Date & time format tokens
The following table defines the token substrings you can use in a date-time
format pattern. A +
character indicates that the previous character can appear
one or more times and still match the pattern. Characters not listed in the
table below are treated as literals, and are output without changes.
h
hh+
m
M
mm
m
, but with a leading 0 for both cases.MM
mmm
mmmm
mmmmmm+
also matches this.mmmmm
s
ss
[h+]
[m+]
[s+]
d
dd
ddd
dddd+
y
yy
yyy
yyyy+
a/p
am/pm
0
00
or three digits (milliseconds) with 000
.\
"text"
Date & time format examples
Given the date and time Tuesday, April 5, 2016, 4:08:53.528 PM
, the
following table shows some example patterns and their corresponding date-time
rendering. The second section of the table shows examples of elapsed time
formats for an elapsed time of 3 hours, 13 minutes, 41.255 seconds
:
Date-time Patterns | Tuesday, April 5, 2016, 4:08:53.528 PM
|
---|---|
h:mm:ss.00 a/p
|
4:08:53.53 p |
hh:mm A/P".M."
|
04:08 P.M. |
yyyy-mm-dd
|
2016-04-05 |
mmmm d \[dddd\]
|
April 5 [Tuesday] |
h PM, ddd mmm dd
|
4 PM, Tue Apr 05 |
dddd, m/d/yy at h:mm
|
Tuesday, 4/5/16 at 16:08 |
Elapsed Time Patterns | 3 hours, 13 minutes, 41.255 seconds
|
[hh]:[mm]:[ss].000
|
03:13:41.255 |
[mmmm]:[ss].000
|
0193:41.255 |
Number format patterns
A number format pattern is a string of token substrings that, when parsed, are replaced with the corresponding number representations. A number format pattern can consist of up to four sections, separated by semicolons, which define the separate formats used for positive numbers, negative numbers, zero, and text (in that order):
[POSITIVE FORMAT];[NEGATIVE FORMAT];[ZERO FORMAT];[TEXT FORMAT]
You don't need to include all four sections in a format. If you only include one section, that format is used for all values. Using two sections causes the first format to be applied to zero and positive numbers and the second format to negative numbers. Using three sections defines separate formats for positive, negative, and zero numbers. For example:
[NUMBER FORMAT] [POSITIVE/ZERO FORMAT];[NEGATIVE FORMAT] [POSITIVE FORMAT];[NEGATIVE FORMAT];[ZERO FORMAT]
However, if there are two or more sections and the final section is a text format, that section is treated as the text format and the others behave as if there's one less section. Thus, by including a final text format it's possible to define formats such as:
[POSITIVE/ZERO FORMAT];[NEGATIVE FORMAT];[TEXT FORMAT] [NUMBER FORMAT];[TEXT FORMAT]
The parsing of a format into sections occurs before other parsing, so it's possible to have a date or time format included as one of the sections (though this is of limited utility).
Number format tokens
The following table defines the token substrings you can use in a format section to define how to represent the values for that section.
0
00.0
renders the number 3 as " 03.0
".#
##0
renders the number 12 as " 12
".?
???.???
renders the number 12.4 as:
" 12.4
"..
#0.#
renders the number 3 as " 3.
".%
#%
renders the number 0.25 as " 25%
".,
0
, #
, or ?
), it renders the entire number
with grouping separators (grouping by the thousands). If it follows
the digit characters, it scales the digits by one thousand per
comma (for example, the format #0.0,,
renders the number
12,200,000 as 12.2
).E-
E
used for the non-exponent
portion and the formatting to the right of the E used for the exponent
portion. E+
shows a +
sign for positive
exponents. E-
only shows a sign for negative exponents. If
lowercase is used, the output e
is in lowercase as well.
For example, the number format ##0.00#E+##
renders the number 0.0000001 as " 100.00E-9
".E+
e-
e+
/
0
, #
, or ?
), it treats those digit groups as a
fractional format. For example, the number format 0 #/#
renders
the number 23.25 as 23 1/4
. The denominator
can also be a literal integer, in which case it enforces that integer as
the denominator. The number format 0 #/8
displays the number 23.25 as 23 2/8
. The fraction part is not
rendered if the numerator becomes 0. The number
23.1 with the number format 0 #/3
renders as just 23
(because the 0.1 is rounded to 0/3). /
is
not compatible with scientific format or a format with a decimal point
in it.*
_
\
\#0
renders the number 10 as " #10
"."text"
@
$ - + ( ) : space
Meta instructions
In addition, each of the format sections can have optional meta instructions,
enclosed in []
characters, that precede the format and provide
additional instructions. There are two meta instruction types and a given
section can use both:
Instruction | Description |
---|---|
[ condition
]
|
Replaces the default positive, negative, or zero comparison of the
section with another conditional expression. For example, [<100]”Low”;[>1000]”High”;000
renders the word
“Low” for values below 100, “High” for values above 1000, and a three-digit
number (with leading 0s) for anything in between. Conditions can only be
applied to the first two sub-formats and if a number matches more than one,
it uses the first one it matches. If there's a third format, it's
used for "everything else", otherwise if a number doesn't match either
format, it's rendered as all "#"s filling up the cell width. If it exists, the
fourth format is always used for text. |
[ Color
] or [ Color#
]
|
Causes any value this sub-format renders to appear with the given text color. Valid values for Color are Black, Blue, Cyan, Green, Magenta, Red, White, or Yellow. Valid values for the "#" in Color# are 1–56 (this color palette shows a list of the colors that correspond to each number). Number format colors override any user-entered colors on the cell, but don't override colors set by conditional formatting. |
Number format examples
The following table shows some example patterns and their corresponding formatted number rendering:
####.#
000.0000
#.0#
# ???/???
#,###
0.0,,"M"
0.00e+00
MyText
###0.000;"TEXT: "_(@_)
TEXT: MyText
-1234
0
MyText
[Blue]#,##0;[Red]#,##0;[Green]0.0;[Magenta]_(@_)
1,234
0.0
MyText
32
527
[>1000]"HIGH";[Color43][<=200]"LOW";0000
LOW
0527