本人最近新学python ,用到关于时间和日期的函数,经过一番研究,从网上查找资料,经过测试,总结了一下相关的方法.
import time
import datetime
'''时间转化为时间戳: 2016-11-16 10:53:12 to 1479264792 '''
def TimeToTimeSpan(timeInfo):
timeArray = time.strptime(timeInfo, "%Y-%m-%d %H:%M:%S")
timeSpan = int(time.mktime(timeArray))
return timeSpan
'''把时间戳转化为时间: 1479264792 to 2016-11-16 10:53:12'''
def TimeStampToTime(timestamp):
timeStruct = time.localtime(timestamp)
return time.strftime('%Y-%m-%d %H:%M:%S',timeStruct)
'''把时间戳转化为时间: 1479264792 to 2016-11-16 10:53:12'''
def TimeStampToTime1(timestamp):
temp = datetime.datetime.fromtimestamp(timestamp)
return temp.strftime("%Y-%m-%d %H:%M:%S")
'''获取当前时间 :2016-06-16 13:37:10'''
def GetCurrentTime():
currtTime = str(datetime.datetime.now())
return currtTime[0:19]
'''获取当前时间 :2016-06-16 13:37:10'''
def GetCurrentTime1():
return time.strftime('%Y-%m-%d %H:%M:%S')
'''获取100天前的时间'''
def Get100Time():
currtTime = str(datetime.datetime.now()-datetime.timedelta(days = 100))
return currtTime[0:19]
'''获取当前日期:2016-06-16'''
def GetCurrentDate():
currtTime = str(datetime.datetime.now())
return currtTime[0:4]
'''获取当前日期:2016-06-16'''
def GetCurrentDate1():
return time.strftime('%Y-%m-%d')
'''获取年'''
def GetYear():
return datetime.datetime.now().year
'''获取月'''
def GetMonth():
return datetime.datetime.now().month
'''获取 号'''
def GetDy():
return datetime.datetime.now().day
'''获取当天是星期几'''
def GetWeekDay():
week_day_dict = {
0 : '星期一',
1 : '星期二',
2 : '星期三',
3 : '星期四',
4 : '星期五',
5 : '星期六',
6 : '星期天',}
day = datetime.datetime.now().weekday()
return week_day_dict[day]