1. 時間字串 --> 時間戳
timestr= '2017-06-06 10:00:00'
seconds = time.mktime(time.strptime(timestr, '%y-%m-%d %h:%m:%s'))
將時間元組轉換成時間戳
time.mktime(timetuple) :
將時間戳轉會為時間元組
time.localtime([timestamp]):
2. 時間戳 --> 時間字串
1) time 模組
timestamp = time.time()
timestruct = time.localtime(timestamp)
timestr = time.strftime('%y-%m-%d %h:%m:%s', timestruct)
2) datetime 模組
import datetime
timestamp = 1482374997.55
datetime_struct = datetime.datetime.fromtimestamp(timestamp)
print datetime_struct.strftime('%y-%m-%d %h:%m:%s')
>> 2016-12-22 10:49:57
datetime_struct = datetime.datetime.utcfromtimestamp(timestamp)
print datetime_struct.strftime('%y-%m-%d %h:%m:%s')
>> 2016-12-22 02:49:57
fromtimestamp(timestamp[, tz]):將時間戳轉為當地的時間元組
utcfromtimestamp(timestamp):將時間戳轉為utc的時間元組。以北京為例:utc時間比北京當地時間少8個小時。
3. 時間差計算
def preday_get(d):
"""
返回上一天的日期時間
:return
date_from: 2017-05-01 00:00:00
date_to: 2017-05-01 23:59:59
"""oneday = datetime.timedelta(days=1)
day = d - oneday
date_from = datetime.datetime(day.year, day.month, day.day, 0, 0, 0)
date_to = datetime.datetime(day.year, day.month, day.day, 23, 59, 59)
# print '---'.join([str(date_from), str(date_to)])
return date_from, date_to
def preweek_get(d):
"""
返回上一周的日期時間
:return
date_from: 2017-05-01 00:00:00
date_to: 2017-05-07 23:59:59
"""dayscount = datetime.timedelta(days=d.isoweekday())
dayto = d - dayscount
sixdays = datetime.timedelta(days=6)
dayfrom = dayto - sixdays
date_from = datetime.datetime(dayfrom.year, dayfrom.month, dayfrom.day, 0, 0, 0)
date_to = datetime.datetime(dayto.year, dayto.month, dayto.day, 23, 59, 59)
# print '---'.join([str(date_from), str(date_to)])
return date_from, date_to
def premonth_get(d):
"""
返回上個月第乙個天和最後一天的日期時間
:return
date_from: 2017-04-01 00:00:00
date_to: 2017-04-30 23:59:59
"""dayscount = datetime.timedelta(days=d.day)
dayto = d - dayscount
date_from = datetime.datetime(dayto.year, dayto.month, 1, 0, 0, 0)
date_to = datetime.datetime(dayto.year, dayto.month, dayto.day, 23, 59, 59)
# print '---'.join([str(date_from), str(date_to)])
return date_from, date_to
def nextmonth_get(d):
"""
返回下個月第乙個天
:return date: 2017-06-01 00:00:00
"""date = datetime.datetime(d.year, d.month+1, 1, 0, 0, 0)
# print '---'.join([str(date_from), str(date_to)])
return date
def nextweek_get(d):
"""
返回下週第乙個天
:return date: 2017-06-01 00:00:00
"""dayscount = datetime.timedelta(days=d.isoweekday())
dayfrom = d - dayscount
stepdate = datetime.timedelta(days=8)
dayto = dayfrom + stepdate
date = datetime.datetime(dayto.year, dayto.month, dayto.day, 00, 00, 00)
return date
4. 任意時間字串轉換時間物件
import time
from dateutil import parser
time_string = time.ctime() # 'thu dec 22 10:35:25 2016',這裡可以是任意的時間格式
datetime_struct = parser.parse(time_string)
print type(datetime_struct) #
print datetime_struct.strftime('%y-%m-%d %h:%m:%s') # 2016-12-22 13:58:59
python 時間處理
django,pytz環境下處理時區問題 datetime obj.replace tzinfo pytz.utc astimezone pytz.timezone settings.time zone 建構函式 date類表示乙個日期。日期由年 月 日組成 地球人都知道 date類的建構函式如下 ...
Python時間處理
使用pandas的read csv函式,parse dates 直接將檔案中的一列解析為時間,index col 將一列作為索引,時間列作為索引時,索引列型別為datetime64。在此dataframe新增一行新的時間資料 重取樣指的是將時間序列從乙個頻率轉換到另乙個頻率的過程將高頻率資料聚合到低...
python 時間處理
deftimestamp to datetime timestamp 將 13 位整數的毫秒時間戳轉化成本地普通時間 datetime 格式 paramtimestamp 13 位整數的毫秒時間戳 1456402864242 return 返回 datetime 格式 2016 02 25 20 2...