Warning: Illegal string offset 'top' in /www/wwwroot/_new/defjia/wp-content/themes/Snape/single.php on line 45
多摘自http://www.wklken.me/posts/2015/03/03/python-base-datetime.html#_1
#五种时间表示方法
from datetime import *
datetime = datetime.now()
#datetime.datetime(2016, 12, 26, 0, 53, 7, 510071)
type(datetime)
#<class ‘datetime.datetime’>
date = datetime.now().date()
#datetime.date(2016, 12, 26)
type(date)
#<class ‘datetime.date’>
str_ = datetime.now().strftime(“%Y-%m-%d %H:%M:%S”)
#’2016-12-26 00:54:14′import time
time = time.time()
#1482684884.7402322
type(time)
#<class ‘float’>
localtime = localtime()
#time.struct_time(tm_year=2016, tm_mon=12, tm_mday=26, tm_hour=0, tm_min=56, tm_sec=3, tm_wday=0, tm_yday=361, tm_isdst=0)
type(localtime)
#<class ‘time.struct_time’>from datetime import *
datetime = datetime.now()
#datetime.datetime(2015, 1, 12, 23, 26, 24, 475680)
today = date.today()
#datetime.date(2015, 1, 12)
today_add = date.today() + timedelta(days=1)
#datetime.date(2015, 1, 13)
day_start = datetime.combine(date.today(), time.min)
#datetime.datetime(2015, 1, 12, 0, 0)
day_end = datetime.combine(date.today(), time.max)
#datetime.datetime(2015, 1, 12, 23, 59, 59, 999999)
delta = (datetime(2015,1,13,12,0,0) – datetime.now()).total_seconds()
#44747.768075
week_end = today + timedelta(6 – today.weekday())
#datetime.date(2015, 1, 18)#Demo1 — get the last day of current month
>>> 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_day
datetime.date(2015, 1, 31)#Demo2 — get the last day of last month
>>> import datetime
>>> today = datetime.date.today()
>>> first = datetime.date(day=1, month=today.month, year=today.year)
>>> lastMonth = first – datetime.timedelta(days=1)#convert between datetime, date, timestamp, timetuple, string
datetime.now().date()#1.1 datetime -> date
datetime.combine(date.today(), time())#1.2 date -> datetime
time.mktime(datetime.now().timetuple())#2.1 datetime -> timestamp
datetime.fromtimestamp(1421077403.0)#2.2 timestamp -> datetime
datetime.now().timetuple()#3.1 datetime -> timetuple
timetuple => timestamp => datetime #3.2 timetuple -> datetime
datetime.now().strftime(“%Y-%m-%d %H:%M:%S”)#4.1 datetime -> string
datetime.strptime(“2014-12-31 18:20:10”, “%Y-%m-%d %H:%M:%S”)#4.2 string -> datetime
Warning: Illegal string offset 'footer' in /www/wwwroot/_new/defjia/wp-content/themes/Snape/single.php on line 49