博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Python 中关于时间格式的知识点

Posted on 2021-04-19 18:14  小翁好  阅读(603)  评论(0编辑  收藏  举报
一:时间,日期,时间戳之间转换

1.将字符串的时间转换为时间戳

a = "2013-10-10 23:40:00"
将其转换为时间数组
import time
timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
转换为时间戳:
timeStamp = int(time.mktime(timeArray))
timeStamp == 1381419600


2.字符格式更改
如a = "2013-10-10 23:40:00",想改为 a = "2013/10/10 23:40:00"
方法:先转换为时间数组,然后转换为其他格式
timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray


3.时间戳转换为指定格式日期:
方法一:
利用localtime()转换为时间数组,然后格式化为需要的格式,如
timeStamp = 1381419600
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
otherStyletime == "2013-10-10 23:40:00"

方法二:
import datetime
timeStamp = 1381419600
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
otherStyletime == "2013-10-10 23:40:00"


4.获取当前时间并转换为指定日期格式
方法一:
import time
获得当前时间时间戳
now = int(time.time()) ->这是时间戳
转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S"
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)

方法二:
import datetime
获得当前时间
now = datetime.datetime.now() ->这是时间数组格式
转换为指定的格式:
otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S")


5.获得三天前的时间
方法:
import time
import datetime
先获得时间数组格式的日期
threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 3))
转换为时间戳:
timeStamp = int(time.mktime(threeDayAgo.timetuple()))
转换为其他字符串格式:
otherStyleTime = threeDayAgo.strftime("%Y-%m-%d %H:%M:%S")
注:timedelta()的参数有:days,hours,seconds,microseconds

6.给定时间戳,计算该时间的几天前时间:
timeStamp = 1381419600
先转换为datetime
import datetime
import time
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
threeDayAgo = dateArray - datetime.timedelta(days = 3)
参考5,可以转换为其他的任意格式了

7. datetime.strftime(format),将datetime对象,格式化输出成字符串。

>>> import datetime
>>> dt = datetime.datetime.strptime('201311261010', '%Y%m%d%H%M%S')
>>> dt.strftime('%Y-%m-%d %H:%M:%S')
'2013-11-26 10:01:00'


阿斯顿发

二:判断星期几和最近前后几天:


import datetime import calendar from chinese_calendar import is_workday import pandas as pd # 判断是否为星期1 def is_monday(): today = datetime.datetime.now().weekday() if today == 0: return True # 判断是否为星期2 def is_tuesday(): today = datetime.datetime.now().weekday() if today == 1: return True # 判断是否为星期3 def is_wednesday(): today = datetime.datetime.now().weekday() if today == 2: return True # 判断是否为星期4 def is_thursday(): today = datetime.datetime.now().weekday() if today == 3: return True # 判断是否为星期5 def is_friday(): today = datetime.datetime.now().weekday() if today == 4: return True # 判断是否为星期6 def is_saturday(): today = datetime.datetime.now().weekday() if today == 5: return True # 判断是否为星期日 def is_sunday(): today = datetime.datetime.now().weekday() if today == 6: return True # 判断上几工作日 def is_word_day(date_num): day = datetime.datetime.now() + datetime.timedelta(days=date_num) res = is_workday(day) if res: return day.strftime("%Y-%m-%d") else: date_num += 1 return is_word_day(date_num) # 上一自然日 def up_day(): return (datetime.datetime.now() + datetime.timedelta(days=-1)).strftime("%Y-%m-%d") # 获取指定前后天 def get_day(date_num): day = datetime.datetime.now() + datetime.timedelta(days=date_num) return day.day # 获取上个月末 def before_month_last_day(): today = datetime.datetime.now() first = datetime.date(day=1, month=today.month, year=today.year) last_day = first - datetime.timedelta(days=1) return last_day.strftime("%Y%m%d") # 获取上几工作日 def get_2up_work(up_date=1, is_work=0, date=1): ''' :param up_date:固定参数值 :param is_work:固定参数值
:param date:可以用来调整前几天工作日 :return: ''' day = datetime.datetime.now() + datetime.timedelta(days=-up_date) res = is_workday(day) if res: if is_work == date: return day.strftime("%Y%m%d")
is_work+=1
up_date+=1 return get_2up_work(up_date, is_work,date) else: up_date += 1 return get_2up_work(up_date, is_work,date) # 获取上个月初 def before_month_fristday(): today = datetime.datetime.now()
if today.month==1:
first=datetime.date(day=1,month=12,year=today.year-1)
else: first = datetime.date(day=1, month=today.month - 1, year=today.year) return first.strftime("%Y%m%d") # 获取上个星期几 def get_up_day(day=1): ''' :param day: 获取上个星期几,1-7 :return: ''' cal_dict = {1: calendar.MONDAY, 2: calendar.TUESDAY, 3: calendar.WEDNESDAY, 4: calendar.THURSDAY, 5: calendar.FRIDAY, 6: calendar.SATURDAY, 7: calendar.SUNDAY} date = datetime.datetime.now() oneday = datetime.timedelta(days=1) while date.weekday() != cal_dict.get(day, calendar.MONDAY): date -= oneday return date.strftime("%Y%m%d")