一、獲取當前日期和時間
import datetime
#獲取當前日期
today_date = datetime.datetime.now().strftime('%y-%m-%d')
print today_date
#獲取當前時間
today_time = datetime.datetime.now().strftime('%h:%m:%s')
print today_time
輸出:
2018-02-09
10:09:26
二、日期字串轉datetime
#日期字串轉datetime
date_str = '2018-2-9'
date_datetime = datetime.datetime.strptime(date_str, '%y-%m-%d')
print date_datetime
#時間字串轉datetime,,比較有意思,預設給的日期是1900-01-01,注意不是當前日期
time_str = '10:05:00'
time_datetime = datetime.datetime.strptime(time_str, '%h:%m:%s')
print time_datetime
#日期時間字串轉datetime
full_str = '2018-2-9 10:05:00'
t_datetime = datetime.datetime.strptime(full_str, '%y-%m-%d %h:%m:%s')
print t_datetime
輸出:2018-02-09 00:00:00
1900-01-01 10:05:00
2018-02-09 10:05:00
三、獲取當前時間戳
import time
#獲取當前時間戳(整型)
timestamp = int(time.time())
print timestamp
輸出:
1518142166
四、時間戳轉日期和時間
#時間戳轉日期和時間
timestamp = 1518142166
localtime = time.localtime(timestamp)
print localtime
date_str = time.strftime('%y-%m-%d %h:%m:%s', localtime)
print date_str
輸出:
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=9, tm_hour=10, tm_min=9, tm_sec=26, tm_wday=4, tm_yday=40, tm_isdst=0)
2018-02-09 10:09:26
五、日期字串轉時間戳
#日期字串轉時間戳
full_str = '2018-2-9 10:05:00'
timearray = time.strptime(full_str, '%y-%m-%d %h:%m:%s')
timestamp = int(time.mktime(timearray))
print timestamp
輸出:
1518141900
Python中日期格式轉換
import time import datetime deftime format self,time str 統一時間格式 param time str return time str time.strptime time str,y m d time str datetime.datetime...
MySQL中日期獲取
select last day curdate 獲取本月最後一天 select date add curdate interval day curdate 1 day 獲取本月第一天 select date add curdate day curdate 1,interval 1 month 獲取下...
HIVE中日期轉換
本來是sql查出來是兩條資料 select groupid,shopid,shopname,deliverytime,sku,transport type from ods amz group fee where dt 2020 12 11 and sku fba syhg372 30cm mm表示...