geekdoc-python-zh/docs/py4b/python-strftime-and-strptim...

42 lines
1.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Python 中的基本日期和时间类型
> 原文:<https://www.pythonforbeginners.com/basics/python-strftime-and-strptime>
## 日期和时间
date、datetime 和 time 对象都支持 strftime(format)方法,以便在显式格式字符串的控制下创建表示时间的字符串。
以下是格式代码及其指令和含义的列表。
```py
%a Locales abbreviated weekday name.
%A Locales full weekday name.
%b Locales abbreviated month name.
%B Locales full month name.
%c Locales appropriate date and time representation.
%d Day of the month as a decimal number [01,31].
%f Microsecond as a decimal number [0,999999], zero-padded on the left
%H Hour (24-hour clock) as a decimal number [00,23].
%I Hour (12-hour clock) as a decimal number [01,12].
%j Day of the year as a decimal number [001,366].
%m Month as a decimal number [01,12].
%M Minute as a decimal number [00,59].
%p Locales equivalent of either AM or PM.
%S Second as a decimal number [00,61].
%U Week number of the year (Sunday as the first day of the week)
%w Weekday as a decimal number [0(Sunday),6].
%W Week number of the year (Monday as the first day of the week)
%x Locales appropriate date representation.
%X Locales appropriate time representation.
%y Year without century as a decimal number [00,99].
%Y Year with century as a decimal number.
%z UTC offset in the form +HHMM or -HHMM.
%Z Time zone name (empty string if the object is naive).
%% A literal '%' character.
```
## strftime() vs strptime()
strptime()–字符串“解析”时间–用于将字符串转换为日期/时间对象。使用它将日期字符串解析为日期/时间对象。
strftime()–字符串“格式”时间–用于格式化日期对象。当您想要格式化日期时,请使用此选项。