Python手冊(Standard Library)--datetime+time+calendar
阿新 • • 發佈:2019-02-05
datetime
datetime模組定義了6個類 | |
---|---|
datetime.date | 表示日期的類 |
datetime.datetime | 表示日期時間的類 |
datetime.time | 表示時間的類 |
datetime.timedelta | 表示時間間隔 |
datetime.tzinfo | 時區的相關資訊 |
datetime.timezone | 將tzinfo抽象基類實現為UTC固定偏移量的類 |
from datetime import date,datetime,time,timedelta
datetime.timedelta
支援數學運算
datetime.timedelta( days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
Attribute | read-only |
---|---|
days | Between -999999999 and 999999999 inclusive |
seconds | Between 0 and 86399 inclusive |
microseconds | Between 0 and 999999 inclusive |
方法 | |
total_seconds() | 返回持續時間中包含的總秒數 |
datetime.date
date(year,month,day)
date.today()
返回today(datetime.date類)date.fromtimestamp(timestamp)
由時間戳轉化date.fromordinal(ordinal)
timestamp(時間戳)是指格林威治時間1970年01月01日00時00分00秒起至現在的總秒數。
Attribute | read-only |
---|---|
date.year | Between MINYEAR and MAXYEAR inclusive. |
date.month | Between 1 and 12 inclusive. |
date.day | Between 1 and the number of days in the given month of the given year. |
運算
date2 = date1 + timedelta
date2 = date1 - timedelta
timedelta = date1 - date2
date1 < date2
方法 | 說明 |
---|---|
replace(year=self.year, month=self.month, day=self.day) | 替換給定日期,但不改變原日期 |
timetuple() | 返回 time.struct_time物件(時間元祖) |
toordinal() | 迴歸原始日期 |
weekday() | Return the day of the week as an integer, where Monday is 0 and Sunday is 6 |
isoweekday() | Return the day of the week as an integer, where Monday is 1 and Sunday is 7 |
isocalendar() | Return a 3-tuple, (ISO year, ISO week number, ISO weekday) |
isoformat() | Return a string ‘YYYY-MM-DD’ |
ctime() | return a string (date(2002, 12, 4).ctime() == 'Wed Dec 4 00:00:00 2002' ) |
strftime(format) | 返回指定格式字元 |
datetime.datetime
datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
建立 | 說明 |
---|---|
datetime.today() |
返回當天date |
datetime.now() |
返回當前系統時間 |
datetime.fromtimestamp(timestamp, tz=None) |
根據時間戮返回datetime物件 |
datetime.fromordinal(ordinal) |
|
datetime.combine(date, time, tzinfo=self.tzinfo) |
date物件和time物件組合成新的datetime物件 |
datetime.strptime(date_string, format) |
字串格式建立 |
datetime.strftime() |
轉換為字元格式 |
Instance attributes | (read-only) |
---|---|
year | Between MINYEAR and MAXYEAR inclusive. |
month | Between 1 and 12 inclusive. |
day | Between 1 and the number of days in the given month of the given year. |
hour | In range(24). |
minute | In range(60). |
second | In range(60). |
microsecond | In range(1000000).(微秒) |
tzinfo | 時區 |
fold |
運算
datetime2 = datetime1 + timedelta
datetime2 = datetime1 - timedelta
timedelta = datetime1 - datetime2
datetime1 < datetime2
方法 | 說明 |
---|---|
date() | 返回date物件 |
time() | 返回time物件 |
replace() | 替換 |
ctime() | 返回格式如 Sun Apr 16 00:00:00 2017 |
timetuple() | 返回time.struct_time物件 |
utctimetuple() | |
toordinal() | |
timestamp() | 返回時間戳(float) |
weekday() | Monday is 0 and Sunday is 6 |
isoweekday() | Monday is 1 and Sunday is 7 |
isocalendar() | Return a 3-tuple, (ISO year, ISO week number, ISO weekday) |
isoformat(sep=‘T’, timespec=‘auto’) | Return a string ‘YYYY-MM-DDTHH:MM:SS.mm’ |
ctime() | return a string (‘Wed Dec 4 20:30:40 2002’) |
strftime(format) | 由日期格式轉化為字串格式 |
關於時區的方法暫時不計入
datetime.time
time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
Instance attributes | (read-only) |
---|---|
hour | In range(24). |
minute | In range(60). |
second | In range(60). |
microsecond | In range(1000000). |
tzinfo | |
fold |
方法 | 說明 |
---|---|
replace(hour=self.hour, minute=self.minute, second=self.second, microsecond=self.microsecond, tzinfo=self.tzinfo, * fold=0) | |
isoformat(timespec=‘auto’) | |
strftime(format) | 轉字元格式 |
tzname() | 返回時區名字 |
utcoffset() | 返回時區的時間偏移量 |
dst() |
python中時間日期格式化符號
格式 | 說明 | Example |
---|---|---|
%a | 週日期縮寫 | Sun, Mon, …, Sat (en_US); |
%A | 週日期全稱 | Sunday, Monday, …, Saturday (en_US); |
%w | 週數字 | 0, 1, …, 6 |
%d | 月中天數 | 01, 02, …, 31 |
%b | 月份縮寫 | Jan, Feb, …, Dec (en_US); |
%B | 月份全稱 | January, February, …, December (en_US); |
%m | 月份數字 | 01, 02, …, 12 |
%y | 年數字,兩位 | 00, 01, …, 99 |
%Y | 年數字,四位 | 0001, 0002, …, 2013, 2014, …, 9998, 9999 |
%H | 24小時制 | 00, 01, …, 23 |
%I | 12小時制 | 01, 02, …, 12 |
%p | AM or PM | AM, PM (en_US); |
%M | 分鐘 | 00, 01, …, 59 |
%S | 秒 | 00, 01, …, 59 |
%f | 微秒 | 000000, 000001, …, 999999 |
%z | UTC offset in the form +HHMM or -HHMM | (empty), +0000, -0400, +1030 |
%Z | 時區名 | (empty), UTC, EST, CST |
%j | 年中的天數 | 001, 002, …, 366 |
%U | 年中的週日期(週日為第一天) | 00, 01, …, 53 |
%W | 年中的週日期(週一為第一天) | 00, 01, …, 53 |
%c | date and time | Tue Aug 16 21:30:00 1988 (en_US); |
%x | date | 08/16/88 (None) 08/16/1988 (en_US) |
%X | time | 21:30:00 (en_US); |
%% | %'字元 | % |
%G | ISO 8601 year | 0001, 0002, …, 2013, 2014, …, 9998, 9999 |
%u | ISO 8601 weekday | 1, 2, …, 7 |
%V | ISO 8601 week | 01, 02, …, 53 |
time
函式 | |
---|---|
time.clock() | 用CPU花費的時間(秒) |
time.sleep(secs) |
time.struct_time(時間戳類)
屬性 | 說明 |
---|---|
tm_year | yyyy |
tm_mon | 1 到 12 |
tm_mday | 1 到 31 |
tm_hour | 0 到 23 |
tm_min | 0 到 59 |
tm_sec | 0 到 61 (60或61 是閏秒) |
tm_wday | 0到6 (0是週一) |
tm_yday | 1 到 366 |
tm_isdst | 1(夏令時)0(不是夏令時)-1(未知) |
calendar
星期一是預設的每週第一天,星期天是預設的最後一天。更改設定需呼叫calendar.setfirstweekday()
函式。
calendar函式 | |
---|---|
calendar.calendar(year,w=2,l=1,c=6) | 年日曆 |
calendar.firstweekday() | 返回當前每週起始日期的設定 |
calendar.month(year,month) | 月日曆 |
calendar.isleap(year) | 是否閏年 |
calendar.leapdays(y1,y2) | 返回在Y1,Y2兩年之間的閏年總數 |
calendar.monthrange(year, month) | Returns weekday of first day of the month and number of days in month |
calendar.weekday(year,month,day) | Returns the day of the week (0 is Monday) |