DATE、DATETIME 和 TIMESTAMP 类型是相关的。本节描述它们的特点,它们的相似之处以及它们的不同之处。MySQL 可以识别多种格式的 DATE、DATETIME 和 TIMESTAMP 值,这些格式在 第 11.1.3 节,“日期和时间字面量” 中有描述。对于 DATE 和 DATETIME 范围描述中,“支持” 意味着虽然早期的值可能有效,但不保证一定可用。The DATE, DATETIME, and
TIMESTAMP types are related. This section
describes their characteristics, how they are similar, and how
they differ. MySQL recognizes DATE,
DATETIME, and TIMESTAMP
values in several formats, described in
Section 11.1.3, “Date and Time Literals”. For the
DATE and DATETIME range
descriptions, “supported” means that although
earlier values might work, there is no guarantee.
DATE 类型用于只包含日期部分而不包含时间部分的值。MySQL 以 ' 格式检索和显示 YYYY-MM-DD'DATE 值。支持的范围是 '1000-01-01' 到 '9999-12-31'。The DATE type is used for values with a date
part but no time part. MySQL retrieves and displays
DATE values in
'
format. The supported range is YYYY-MM-DD''1000-01-01'
to '9999-12-31'.
DATETIME 类型用于包含日期和时间部分的值。MySQL 以 DATETIME 格式检索和显示 YYYY-MM-DD
hh:mm:ss 值。支持的范围是 '1000-01-01 00:00:00' 到 '9999-12-31
23:59:59'。The DATETIME type is used for values that
contain both date and time parts. MySQL retrieves and displays
DATETIME values in
' format. The supported range is
YYYY-MM-DD
hh:mm:ss''1000-01-01 00:00:00' to '9999-12-31
23:59:59'.
TIMESTAMP 数据类型用于包含日期和时间部分的值。TIMESTAMP 的范围是 '1970-01-01
00:00:01' UTC 到 '2038-01-19
03:14:07' UTC。The TIMESTAMP data type is used for values
that contain both date and time parts.
TIMESTAMP has a range of '1970-01-01
00:00:01' UTC to '2038-01-19
03:14:07' UTC.
A DATETIME或者TIMESTAMP
value can include a trailing fractional seconds part in up to
microseconds (6 digits) precision. In particular, any fractional
part in a value inserted into a DATETIME或者TIMESTAMP column is stored rather than
discarded. With the fractional part included, the format for
these values is ',
the range for YYYY-MM-DD
hh:mm:ss[.fraction]'DATETIME values is
'1000-01-01 00:00:00.000000'到'9999-12-31 23:59:59.499999', and the range
for TIMESTAMP values is '1970-01-01
00:00:01.000000'到'2038-01-19
03:14:07.499999'. The fractional part should always be
separated from the rest of the time by a decimal point; no other
fractional seconds delimiter is recognized. For information
about fractional seconds support in MySQL, see
Section 13.2.6, “Fractional Seconds in Time Values”.A DATETIME or TIMESTAMP
value can include a trailing fractional seconds part in up to
microseconds (6 digits) precision. In particular, any fractional
part in a value inserted into a DATETIME or
TIMESTAMP column is stored rather than
discarded. With the fractional part included, the format for
these values is ',
the range for YYYY-MM-DD
hh:mm:ss[.fraction]'DATETIME values is
'1000-01-01 00:00:00.000000' to
'9999-12-31 23:59:59.499999', and the range
for TIMESTAMP values is '1970-01-01
00:00:01.000000' to '2038-01-19
03:14:07.499999'. The fractional part should always be
separated from the rest of the time by a decimal point; no other
fractional seconds delimiter is recognized. For information
about fractional seconds support in MySQL, see
Section 13.2.6, “Fractional Seconds in Time Values”.
TIMESTAMP 和 DATETIME 数据类型提供自动初始化和更新为当前日期和时间的功能。更多信息,请参见 第 13.2.5 节,“TIMESTAMP 和 DATETIME 的自动初始化和更新”。The TIMESTAMP and DATETIME
data types offer automatic initialization and updating to the
current date and time. For more information, see
Section 13.2.5, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.
MySQL 将 TIMESTAMP 值从当前时区转换为 UTC 存储,再从 UTC 转换回当前时区进行检索。(对于其他类型,如 DATETIME,则不会发生此操作。)默认情况下,每个连接的当前时区为服务器时间。时区可以按连接单独设置。只要时区设置保持不变,你检索到的值就与存储的值相同。如果你存储了一个 TIMESTAMP 值,然后更改了时区并检索该值,检索到的值将与存储的值不同。这是因为在两个方向的转换中未使用相同的时区。当前时区可通过 time_zone 系统变量获取。更多信息,请参阅 第 7.1.15 节,“MySQL 服务器时区支持”。MySQL converts TIMESTAMP values from the
current time zone to UTC for storage, and back from UTC to the
current time zone for retrieval. (This does not occur for other
types such as DATETIME.) By default, the
current time zone for each connection is the server's time. The
time zone can be set on a per-connection basis. As long as the
time zone setting remains constant, you get back the same value
you store. If you store a TIMESTAMP value,
and then change the time zone and retrieve the value, the
retrieved value is different from the value you stored. This
occurs because the same time zone was not used for conversion in
both directions. The current time zone is available as the value
of the time_zone system
variable. For more information, see
Section 7.1.15, “MySQL Server Time Zone Support”.
在向表中插入 TIMESTAMP 或 DATETIME 值时,您可以指定时区偏移。有关更多信息和示例,请参见 第 11.1.3 节,“日期和时间字面值”。You can specify a time zone offset when inserting a
TIMESTAMP or DATETIME
value into a table. See
Section 11.1.3, “Date and Time Literals”, for more information
and examples.
如果 SQL 模式允许此转换,无效的 DATE、DATETIME 或 TIMESTAMP 值将被转换为相应类型的“零”值('0000-00-00' 或 '0000-00-00
00:00:00')。具体行为取决于是否启用了严格 SQL 模式和 NO_ZERO_DATE SQL 模式;请参阅 第 7.1.11 节,“服务器 SQL 模式”。Invalid DATE, DATETIME, or
TIMESTAMP values are converted to the
“zero” value of the appropriate type
('0000-00-00' or '0000-00-00
00:00:00'), if the SQL mode permits this conversion.
The precise behavior depends on which if any of strict SQL mode
and the NO_ZERO_DATE SQL mode
are enabled; see Section 7.1.11, “Server SQL Modes”.
您可以在检索 TIMESTAMP 值时使用 CAST() 结合 DATETIME 运算符将其转换为 UTC AT TIME
ZONE 值,如下所示:You can convert TIMESTAMP values to UTC
DATETIME values when retrieving them using
CAST() with the AT TIME
ZONE operator, as shown here:
mysql>SELECT col,>CAST(col AT TIME ZONE INTERVAL '+00:00' AS DATETIME) AS ut>FROM ts ORDER BY id;+---------------------+---------------------+ | col | ut | +---------------------+---------------------+ | 2020-01-01 10:10:10 | 2020-01-01 15:10:10 | | 2019-12-31 23:40:10 | 2020-01-01 04:40:10 | | 2020-01-01 13:10:10 | 2020-01-01 18:10:10 | | 2020-01-01 10:10:10 | 2020-01-01 15:10:10 | | 2020-01-01 04:40:10 | 2020-01-01 09:40:10 | | 2020-01-01 18:10:10 | 2020-01-01 23:10:10 | +---------------------+---------------------+
有关语法和更多示例的完整信息,请参阅 CAST() 函数的说明。For complete information regarding syntax and additional
examples, see the description of the
CAST() function.
注意 MySQL 中日期值解释的某些属性:Be aware of certain properties of date value interpretation in MySQL:
MySQL 允许对以字符串指定的值使用“宽松”格式,其中可以使用任何标点符号作为日期部分或时间部分之间的分隔符。在某些情况下,这种语法可能具有迷惑性。例如,一个值如
'10:11:12'可能因为:看起来像时间值,但如果在日期上下文中使用,则被解释为年份'2010-11-12'。值'10:45:15'会被转换为'0000-00-00',因为'45'不是有效的月份。MySQL permits a “relaxed” format for values specified as strings, in which any punctuation character may be used as the delimiter between date parts or time parts. In some cases, this syntax can be deceiving. For example, a value such as'10:11:12'might look like a time value because of the:, but is interpreted as the year'2010-11-12'if used in date context. The value'10:45:15'is converted to'0000-00-00'because'45'is not a valid month.日期和时间部分与分数秒部分之间唯一被识别的分隔符是小数点。The only delimiter recognized between a date and time part and a fractional seconds part is the decimal point.
服务器要求月份和日期的值必须有效,而不仅仅是分别在 1 到 12 和 1 到 31 的范围内。禁用严格模式时,像
'2004-04-31'这样的无效日期会被转换为'0000-00-00'并生成警告。启用严格模式时,无效日期会生成错误。要允许此类日期,请启用ALLOW_INVALID_DATES。有关更多信息,请参见 第 7.1.11 节,“服务器 SQL 模式”。The server requires that month and day values be valid, and not merely in the range 1 to 12 and 1 to 31, respectively. With strict mode disabled, invalid dates such as'2004-04-31'are converted to'0000-00-00'and a warning is generated. With strict mode enabled, invalid dates generate an error. To permit such dates, enableALLOW_INVALID_DATES. See Section 7.1.11, “Server SQL Modes”, for more information.MySQL 不接受包含日期或月份列为零的
TIMESTAMP值,或不为有效日期的值。唯一的例外是特殊的 “零” 值'0000-00-00 00:00:00',前提是 SQL 模式允许此值。具体行为取决于是否启用了严格 SQL 模式和NO_ZERO_DATESQL 模式;详见 第 7.1.11 节,“服务器 SQL 模式”。MySQL does not acceptTIMESTAMPvalues that include a zero in the day or month column or values that are not a valid date. The sole exception to this rule is the special “zero” value'0000-00-00 00:00:00', if the SQL mode permits this value. The precise behavior depends on which if any of strict SQL mode and theNO_ZERO_DATESQL mode are enabled; see Section 7.1.11, “Server SQL Modes”.包含两位数年份的日期是模糊的,因为世纪未知。MySQL 使用以下规则解释两位数年份:Dates containing 2-digit year values are ambiguous because the century is unknown. MySQL interprets 2-digit year values using these rules:
年份值在
00-69范围内时,将变为2000-2069。Year values in the range00-69become2000-2069.年份值在
70-99范围内时,将变为1970-1999。Year values in the range70-99become1970-1999.
另见 第 13.2.9 节,“日期中的两位数年份”。See also Section 13.2.9, “2-Digit Years in Dates”.