官网地址: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
8.1.7. strftime()
and strptime()
Behavior
date
, datetime
, and time
objects all support a strftime(format)
method, to create a string representing the time under the control of an explicit format string. Broadly speaking, d.strftime(fmt)
acts like the time
module’s time.strftime(fmt, d.timetuple())
although not all objects support a timetuple()
method.
Conversely, the datetime.strptime()
class method creates a datetime
object from a string representing a date and time and a corresponding format string. datetime.strptime(date_string, format)
is equivalent to datetime(*(time.strptime(date_string, format)[0:6]))
, except when the format includes sub-second components or timezone offset information, which are supported in datetime.strptime
but are discarded by time.strptime
.
For time
objects, the format codes for year, month, and day should not be used, as time objects have no such values. If they’re used anyway, 1900
is substituted for the year, and 1
for the month and day.
For date
objects, the format codes for hours, minutes, seconds, and microseconds should not be used, as date
objects have no such values. If they’re used anyway, 0
is substituted for them.
The full set of format codes supported varies across platforms, because Python calls the platform C library’s strftime()
function, and platform variations are common. To see the full set of format codes supported on your platform, consult the strftime(3) documentation.
For the same reason, handling of format strings containing Unicode code points that can’t be represented in the charset of the current locale is also platform-dependent. On some platforms such code points are preserved intact in the output, while on others strftime
may raise UnicodeError
or return an empty string instead.
The following is a list of all the format codes that the C standard (1989 version) requires, and these work on all platforms with a standard C implementation. Note that the 1999 version of the C standard added additional format codes.
The exact range of years for which strftime()
works also varies across platforms. Regardless of platform, years before 1900 cannot be used.
|
Weekday as locale’s abbreviated name. |
Sun, Mon, …, Sat (en_US);
So, Mo, …, Sa (de_DE)
|
(1) |
|
Weekday as locale’s full name. |
Sunday, Monday, …, Saturday (en_US);
Sonntag, Montag, …, Samstag (de_DE)
|
(1) |
|
Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. |
0, 1, …, 6 |
|
|
Day of the month as a zero-padded decimal number. |
01, 02, …, 31 |
|
|
Month as locale’s abbreviated name. |
Jan, Feb, …, Dec (en_US);
Jan, Feb, …, Dez (de_DE)
|
(1) |
|
Month as locale’s full name. |
January, February, …, December (en_US);
Januar, Februar, …, Dezember (de_DE)
|
(1) |
|
Month as a zero-padded decimal number. |
01, 02, …, 12 |
|
|
Year without century as a zero-padded decimal number. |
00, 01, …, 99 |
|
|
Year with century as a decimal number. |
1970, 1988, 2001, 2013 |
|
|
Hour (24-hour clock) as a zero-padded decimal number. |
00, 01, …, 23 |
|
|
Hour (12-hour clock) as a zero-padded decimal number. |
01, 02, …, 12 |
|
|
Locale’s equivalent of either AM or PM. |
AM, PM (en_US);
am, pm (de_DE)
|
(1), (2) |
|
Minute as a zero-padded decimal number. |
00, 01, …, 59 |
|
|
Second as a zero-padded decimal number. |
00, 01, …, 59 |
(3) |
|
Microsecond as a decimal number, zero-padded on the left. |
000000, 000001, …, 999999 |
(4) |
|
UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive). |
(empty), +0000, -0400, +1030 |
(5) |
|
Time zone name (empty string if the object is naive). |
(empty), UTC, EST, CST |
|
|
Day of the year as a zero-padded decimal number. |
001, 002, …, 366 |
|
|
Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. |
00, 01, …, 53 |
(6) |
|
Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0. |
00, 01, …, 53 |
(6) |
|
Locale’s appropriate date and time representation. |
Tue Aug 16 21:30:00 1988 (en_US);
Di 16 Aug 21:30:00 1988 (de_DE)
|
(1) |
|
Locale’s appropriate date representation. |
08/16/88 (None);
08/16/1988 (en_US);
16.08.1988 (de_DE)
|
(1) |
|
Locale’s appropriate time representation. |
21:30:00 (en_US);
21:30:00 (de_DE)
|
(1) |
|
A literal |
% |
Notes:
-
Because the format depends on the current locale, care should be taken when making assumptions about the output value. Field orderings will vary (for example, “month/day/year” versus “day/month/year”), and the output may contain Unicode characters encoded using the locale’s default encoding (for example, if the current locale is
ja_JP
, the default encoding could be any one ofeucJP
,SJIS
, orutf-8
; uselocale.getlocale()
to determine the current locale’s encoding). -
When used with the
strptime()
method, the%p
directive only affects the output hour field if the%I
directive is used to parse the hour. -
Unlike the
time
module, thedatetime
module does not support leap seconds. -
%f
is an extension to the set of format characters in the C standard (but implemented separately in datetime objects, and therefore always available). When used with thestrptime()
method, the%f
directive accepts from one to six digits and zero pads on the right.New in version 2.6.
-
For a naive object, the
%z
and%Z
format codes are replaced by empty strings.For an aware object:
%z
utcoffset()
is transformed into a 5-character string of the form +HHMM or -HHMM, where HH is a 2-digit string giving the number of UTC offset hours, and MM is a 2-digit string giving the number of UTC offset minutes. For example, ifutcoffset()
returnstimedelta(hours=-3, minutes=-30)
,%z
is replaced with the string'-0330'
.%Z
If
tzname()
returnsNone
,%Z
is replaced by an empty string. Otherwise%Z
is replaced by the returned value, which must be a string. -
When used with the
strptime()
method,%U
and%W
are only used in calculations when the day of the week and the year are specified.
相关推荐
总的来说,Python的`datetime`模块提供了丰富的功能来处理日期和时间,包括获取当前日期时间、格式化输出、以及计算日期差。无论是简单的日常操作还是复杂的日期计算,`datetime`都能提供强大的支持。了解并熟练掌握...
Python 的 datetime 模块是处理日期和时间的重要工具,它为程序员提供了丰富的功能,包括日期的计算、时间的格式化以及与时间戳的转换等。在这个主题中,我们将深入探讨 datetime 模块的核心概念和常见用法。 1. **...
5. `strftime()`方法用于将`datetime`对象格式化为字符串,可以根据提供的格式化字符串进行定制。 6. `strptime()`函数用于将符合指定格式的字符串解析为`datetime`对象。 7. `timetuple()`方法将`datetime`对象转换...
您可能感兴趣的文章:python日期时间转为字符串或者格式化输出的实例python中日期和时间格式化输出的方法小结Python 时间操作例子和时间格式化参数小结Python简单格式化时间的方法【strftime函数】python 时间戳与...
本文将深入探讨这个模块中的几个关键概念,包括datetime类、timedelta类,以及strftime和strptime这两个重要的格式化函数。 1. **datetime类**: datetime类用于表示日期和时间的组合,其包含年(year)、月...
此外,还可以进行日期的加减运算、比较以及格式化输出,如`print(today.strftime("%Y-%m-%d"))`将日期转换为"年-月-日"的格式。 接下来是SQL数据库。Python有多种库可以与SQL数据库进行交互,如SQLite、MySQL、...
print("格式化后的日期时间:", formatted_datetime) ``` 这里的`%Y-%m-%d %H:%M:%S`指定了输出格式,其中: - `%Y`表示四位数的年份; - `%m`表示月份; - `%d`表示月份中的日期; - `%H`表示小时(24小时制); - ...
输出这些带有时区信息的datetime对象时,可以使用`strftime()`方法以ISO 8601格式(例如`'%Y-%m-%dT%H:%M:%S.%f%z'`)或者`isoformat()`方法进行格式化。注意,`%z`会提供时区的UTC偏移,而`%Z`则会尝试提供时区的...
在Python中进行日期和时间格式化输出是一个常用且非常重要的功能,它允许用户按照特定的格式展示日期和时间数据。在Python的标准库中,`datetime`模块提供了很多处理日期和时间相关的功能。本小结将主要介绍`...
在Python中,通常使用`datetime`模块的`strftime`方法来格式化日期。例如: ```python from datetime import datetime date = datetime.now() formatted_date = date.strftime("%Y-%m-%d %H:%M:%S") print...
Python的datetime模块是用于处理日期和时间的工具,其中包含了多种类,如date、time、datetime和timedelta。本篇文章将详细讲解timedelta类及其在测量程序编制中的应用。 timedelta类是datetime模块中用于表示两个...
比如,我们可以使用datetime类来表示一个特定的日期和时间,然后进行加减运算、格式化输出等操作。另外,还可以使用timedelta类来表示时间间隔,进行日期和时间的加减运算。在秒表程序中,我们可以利用datetime库来...
Python的datetime模块提供了丰富的功能,用于日期和时间的表示、操作以及格式化。本文将详细介绍datetime模块的使用方法,包括日期和时间的创建、运算、格式化和时区处理等,并通过实际代码示例展示其应用。 通过...
在Python编程中,`datetime`模块是一个非常重要的部分,它为处理日期和时间提供了丰富的功能。相较于`time`模块,`datetime`模块提供了更加直观且强大的接口,使得开发者能够更加方便地进行日期和时间的计算与操作。...
前言 关于时间的处理,Python中自带的处理时间的模块就有time 、datetime、calendar,另外还有扩展的第三方库,如dateutil等等。...单个时间格式字符串转换为numpy的datetime对象,可使用datetime64实例化一
`datetime`模块还允许我们进行日期和时间的格式化,以便于输出和读取。 其次,`turtle`模块是一个简单的图形库,最初设计为初学者学习编程的工具。它允许程序员通过简单的命令控制一个虚拟的“乌龟”在屏幕上移动并...
不过,通常在Python这边做好格式化就可以避免这个问题。 总结来说,Python 3中向MySQL插入datetime类型数据的关键步骤包括: 1. 使用`datetime.datetime.now()`获取当前时间。 2. 使用`strftime()`方法将datetime...
### Python使用JSON序列化datetime类型实例解析 #### 引言 在进行Web开发或处理不同系统间的数据交换时,我们经常需要将Python对象序列化为JSON格式,以便于传输和存储。然而,当我们尝试序列化包含`datetime`类型...