# -*-coding: utf-8 -*-
"""
python時間相關
"""
import time
import datetime
目前的 timestamp
timestampNow=time.time()
print timestampNow
>>> 1495355013.86
目前的 datetime
dateNow = datetime.datetime.now()
print dateNow
>>> 2017-05-21 16:23:33.858233
今天的 datetime
dateToday = datetime.date.today()
print dateToday
>>> 2017-05-21
昨天的 datetime
yesterday = datetime.date.today()-datetime.timedelta(1)
print yesterday
>>> 2017-05-20
昨天的 datetime 輸出指定格式字串
yesterday = datetime.date.today()-datetime.timedelta(1)
# dateTime yesterday in %m-%d-%y (月-日-年)
print(yesterday.strftime('%m-%d-%y'))
>>> 05-20-17
n分鐘前的datetime
dateAgo=datetime.datetime.now()-datetime.timedelta(minutes=10)
# dateTime 10 minutes ago
print dateAgo
>>> 2017-05-21 16:13:34.098177
改變時區(UTC+0的時間轉為UTC+8)
dateNow = ((datetime.datetime.now())+datetime.timedelta(hours=8)) #Taiwan is UTC+8
dateNowStr = dateNow.strftime("%Y%m%d") # 輸出成年月日
print dateNowStr
>>> 2017-05-21
n分鐘前的timestamp
timestampAgo=(time.time()-10*60)
# timestamp 10 minutes ago
print timestampAgo
>>> 1495354414.09
得到指定格式的日期與時間
# "格式為 [年/月/日 時:分:秒] 的時間:"
print time.strftime("%Y/%m/%d %H:%M:%S")
>>> 2017/05/21 16:23:34
指定格式的日期字串轉成時間物件
# "字串為 2017/05/21 的時間:"
dateString = "2017/05/21"
dateObj = datetime.datetime.strptime(dateString, "%Y/%m/%d")
print dateObj
>>> 2017-05-21 00:00:00
timestamp 轉 datetime
timestampNow=time.time()
date_time = datetime.datetime.fromtimestamp(timestampNow)
print timestampNow
>>> 1495355014.1
print date_time
>>> 2017-05-21 16:23:34.098222
datetime 轉 timestamp
dtNow=datetime.datetime.now()
print dtNow
>>> 2017-05-21 16:23:34.098243
dtNow_timestamp=time.mktime(dtNow.timetuple())
print dtNow_timestamp
>>> 1495355014.0
以datetime 得 星期幾
def get_week_day(date):
week_day_dict = {
0 : '星期一',
1 : '星期二',
2 : '星期三',
3 : '星期四',
4 : '星期五',
5 : '星期六',
6 : '星期天',
}
day = date.weekday()
return week_day_dict[day]
dateTimeNow = datetime.datetime.now()
weekdayIndex = dateTimeNow.weekday()
weekday = get_week_day(datetime.datetime.now())
print(dateTimeNow)
>>> 2017-05-21 16:23:34.098267
print(weekdayIndex)
>>> 6
print(weekday)
>>> 星期天
Harrah's Resort Southern California - Mapyro
回覆刪除This is a Valley View hotel in 출장안마 Valley Center, California. This Valley View casino 전라북도 출장안마 is owned by the Rincon Band 광명 출장안마 of Luiseno 제주도 출장안마 Indians. View 여수 출장샵 detailed floor plans,