python时间戳及字符串时间相关函数

python时间戳及字符串时间相关函数

使用依赖包

import time
import datetime
import calendar

获取当前时间戳

time_now = calendar.timegm(time.gmtime())
time_now1 = time.time()

时间戳与字符串时间的转换

# 工作中经常用到的格式"%Y-%m-%d %H:%M:%S"

## 1、时间戳转字符串格式时间
def change_time(time_stamp, style="%Y-%m-%d %H:%M:%S"):
    dateArray = datetime.datetime.utcfromtimestamp(time_stamp)
    otherStyleTime = dateArray.strftime(style)
    return otherStyleTime

## 2、字符串格式时间转时间戳
def change_stamp(time_style, style="%Y-%m-%d %H:%M:%S"):
    timeArray = time.strptime(time_style, style)
    timeStamp = int(time.mktime(timeArray))
    return timeStamp

python3获取两日期间的每一天

def getEveryDay(begin_date,end_date):
    # 前闭后闭
    date_list = []
    begin_date = datetime.datetime.strptime(begin_date, "%Y-%m-%d")
    end_date = datetime.datetime.strptime(end_date,"%Y-%m-%d")
    while begin_date <= end_date:
        date_str = begin_date.strftime("%Y-%m-%d")
        date_list.append(date_str)
        begin_date += datetime.timedelta(days=1)
    return date_list

你可能感兴趣的:(python基础,python,时间戳)