datetime <=> stringpython
2. 獲取當天datecode
3. 獲取明天/前N天htm
很久沒寫文了, 繼續清理筆記, 都是一些小結之類的, 源碼分析以及在作系統中一些細節難點等, 後續逐步發
另外打個廣告, 甜品禮物, 座標深圳, 有興趣的同窗能夠給我發郵件, 非python職位會轉到對應同事:)
_ _ _ _ | | | | | | (_) __| | __ _| |_ ___| |_ _ _ __ ___ ___ / _` |/ _` | __/ _ \ __| | '_ ` _ \ / _ \ | (_| | (_| | || __/ |_| | | | | | | __/ \__,_|\__,_|\__\___|\__|_|_| |_| |_|\___|
原則, 以datetime
爲中心, 起點或中轉, 轉化爲目標對象, 涵蓋了大多數業務場景中須要的日期轉換處理
步驟:
1. 掌握幾種對象及其關係 2. 瞭解每類對象的基本操做方法 3. 經過轉化關係轉化
>>> import datetime>>> now = datetime.datetime.now()>>> nowdatetime.datetime(2015, 1, 12, 23, 9, 12, 946118)>>> type(now)<type 'datetime.datetime'>
>>> import time>>> time.time()1421075455.568243
>>> import time>>> time.localtime()time.struct_time(tm_year=2015, tm_mon=1, tm_mday=12, tm_hour=23, tm_min=10, tm_sec=30, tm_wday=0, tm_yday=12, tm_isdst=0)
>>> import datetime>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")'2015-01-12 23:13:08'
>>> import datetime>>> datetime.datetime.now().date()datetime.date(2015, 1, 12)
>>> import datetime>>> datetime.datetime.now()datetime.datetime(2015, 1, 12, 23, 26, 24, 475680)
>>> datetime.date.today()datetime.date(2015, 1, 12)
明天
>>> datetime.date.today() + datetime.timedelta(days=1)datetime.date(2015, 1, 13)
三天前
>>> datetime.datetime.now()datetime.datetime(2015, 1, 12, 23, 38, 55, 492226)>>> datetime.datetime.now() - datetime.timedelta(days=3)datetime.datetime(2015, 1, 9, 23, 38, 57, 59363)
>>> datetime.datetime.combine(datetime.date.today(), datetime.time.min)datetime.datetime(2015, 1, 12, 0, 0)>>> datetime.datetime.combine(datetime.date.today(), datetime.time.max)datetime.datetime(2015, 1, 12, 23, 59, 59, 999999)
>>> (datetime.datetime(2015,1,13,12,0,0) - datetime.datetime.now()).total_seconds()44747.768075
本週
>>> today = datetime.date.today()>>> todaydatetime.date(2015, 1, 12)>>> sunday = today + datetime.timedelta(6 - today.weekday())>>> sundaydatetime.date(2015, 1, 18)
本月
>>> import calendar>>> today = datetime.date.today()>>> _, last_day_num = calendar.monthrange(today.year, today.month)>>> last_day = datetime.date(today.year, today.month, last_day_num)>>> last_daydatetime.date(2015, 1, 31)
獲取上個月的最後一天(可能跨年)
>>> import datetime>>> today = datetime.date.today()>>> first = datetime.date(day=1, month=today.month, year=today.year)>>> lastMonth = first - datetime.timedelta(days=1)
幾個關係之間的轉化
Datetime Object / String / timestamp / time tuple
datetime -> string
>>> import datetime>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")'2015-01-12 23:13:08'
string -> datetime
>>> import datetime>>> datetime.datetime.strptime("2014-12-31 18:20:10", "%Y-%m-%d %H:%M:%S")datetime.datetime(2014, 12, 31, 18, 20, 10)
datetime -> timetuple
>>> import datetime>>> datetime.datetime.now().timetuple()time.struct_time(tm_year=2015, tm_mon=1, tm_mday=12, tm_hour=23, tm_min=17, tm_sec=59, tm_wday=0, tm_yday=12, tm_isdst=-1)
timetuple -> datetime
timetuple => timestamp => datetime [看後面datetime<=>timestamp]
datetime -> date
>>> import datetime>>> datetime.datetime.now().date()datetime.date(2015, 1, 12)
date -> datetime
>>> datetime.date.today()datetime.date(2015, 1, 12)>>> today = datetime.date.today()>>> datetime.datetime.combine(today, datetime.time())datetime.datetime(2015, 1, 12, 0, 0)>>> datetime.datetime.combine(today, datetime.time.min)datetime.datetime(2015, 1, 12, 0, 0)
datetime -> timestamp
>>> now = datetime.datetime.now()>>> timestamp = time.mktime(now.timetuple())>>> timestamp1421077403.0
timestamp -> datetime
>>> datetime.datetime.fromtimestamp(1421077403.0)datetime.datetime(2015, 1, 12, 23, 43, 23)