python日期相關(guān)操作實例小結(jié)
本文實例講述了python日期相關(guān)操作。分享給大家供大家參考,具體如下:
用 Python 做項目時,經(jīng)常會遇到與日期轉(zhuǎn)換相關(guān),日期計算相關(guān)的功能,動不動就要去查python手冊,感覺麻煩,因此把自己常用的一些東西,總結(jié)了一下,總體說來到目前為止遇到如下一些需求:
1. 用python 把datetime轉(zhuǎn)成字符串
2. 用python 把字符串轉(zhuǎn)成datetime
3. 用python 把字符串轉(zhuǎn)成時間戳形式
4. 用python 把時間戳轉(zhuǎn)成字符串形式
5. 用python 把把datetime類型轉(zhuǎn)外時間戳形式
6. 用python 計算兩個日期之間的差
7. 用python 計算兩個日期的 timestamp 差值
8. 用python 比較兩個日期的大小
9. 指定日期加上 一個時間段,天,小時,或分鐘之后的日期
下面是測試代碼:
#coding:utf-8
'''
base on python 2.7
Created on 2012-12-14
@author: yiahomen.com
'''
import datetime
import time
def datetime_toString(dt):
"""把datetime轉(zhuǎn)成字符串"""
return dt.strftime("%Y-%m-%d")
def string_toDatetime(string):
"""把字符串轉(zhuǎn)成datetime"""
return datetime.datetime.strptime(string, "%Y-%m-%d")
def string_toTimestamp(strTime):
"""把字符串轉(zhuǎn)成時間戳形式"""
return time.mktime(string_toDatetime(strTime).timetuple())
def timestamp_toString(stamp):
"""把時間戳轉(zhuǎn)成字符串形式"""
return time.strftime("%Y-%m-%d-%H", time.localtime(stamp))
def datetime_toTimestamp(dateTime):
"""把datetime類型轉(zhuǎn)外時間戳形式"""
return time.mktime(dateTime.timetuple())
def substract_DateTime(dateStr1,dateStr2):
""" 返回兩個日期之間的差 """
d1=string_toDatetime(dateStr1)
d2=string_toDatetime(dateStr2)
return d2-d1
def substract_TimeStamp(dateStr1,dateStr2):
""" 兩個日期的 timestamp 差值 """
ts1= string_toTimestamp(dateStr1)
ts2= string_toTimestamp(dateStr2)
return ts1-ts2
def compare_dateTime(dateStr1,dateStr2):
"""兩個日期的比較, 當(dāng)然也可以用timestamep方法比較,都可以實現(xiàn)."""
date1 = string_toDatetime(dateStr1)
date2 = string_toDatetime(dateStr2)
return date1.date()>date2.date()
def dateTime_Add(dateStr,days=0,hours=0,minutes=0):
""" 指定日期加上 一個時間段,天,小時,或分鐘之后的日期 """
date1= string_toDatetime(dateStr)
return date1+datetime.timedelta(days=days,hours=hours,minutes=minutes)
if __name__=='__main__':
print substract_DateTime('2012-12-12','2012-01-01')
#-346 days, 0:00:00
print substract_DateTime('2012-12-12','2012-01-01').days
#-346
print substract_TimeStamp('2012-12-12','2012-01-01')
#29894400.0
print substract_TimeStamp('2012-12-12','2012-01-01')/(24*60*60),'天'
#346.0 天
print compare_dateTime('2012-12-12','2012-01-01')
#True
print dateTime_Add('2012-12-12',days=10,hours=5,minutes=10)
#2012-12-22 05:10:00
備注:根據(jù)當(dāng)前日期,對年月進(jìn)行加減的方法
import calendar, datetime def increment_month(when): days = calendar.monthrange(when.year, when.month)[1] return when + datetime.timedelta(days=days) now = datetime.datetime.now() print 'It is now %s' % now print 'In a month, it will be %s' % increment_month(now)
PS:這里再為大家推薦幾款關(guān)于日期與天數(shù)計算的在線工具供大家使用:
在線日期/天數(shù)計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線萬年歷日歷:
http://tools.jb51.net/bianmin/wannianli
在線陰歷/陽歷轉(zhuǎn)換工具:
http://tools.jb51.net/bianmin/yinli2yangli
Unix時間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python日期與時間操作技巧總結(jié)》、《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
tensorflow構(gòu)建BP神經(jīng)網(wǎng)絡(luò)的方法
這篇文章主要為大家詳細(xì)介紹了tensorflow構(gòu)建BP神經(jīng)網(wǎng)絡(luò)的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
python字典dict中常用內(nèi)置函數(shù)的使用
本文主要介紹了python字典dict中常用內(nèi)置函數(shù)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Python方法中self和parent參數(shù)用法詳解
大家好!今天我們來聊一個 Python 中常見但可能讓人困惑的話題:方法參數(shù)里的 self 和 parent,在這篇博客中,我會分三個章節(jié)逐步講解它們的含義、作用和實際應(yīng)用,帶你徹底搞懂它們,需要的朋友可以參考下2025-04-04
pycharm社區(qū)版安裝django并創(chuàng)建一個簡單項目的全過程
社區(qū)版的pycharm跟專業(yè)版的pycharm應(yīng)用差別還是不太大,下面這篇文章主要給大家介紹了關(guān)于pycharm社區(qū)版安裝django并創(chuàng)建一個簡單項目的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
Python中axis=0與axis=1指的方向有什么不同詳解
對數(shù)據(jù)進(jìn)行操作時,經(jīng)常需要在橫軸方向或者數(shù)軸方向?qū)?shù)據(jù)進(jìn)行操作,這時需要設(shè)定參數(shù)axis的值,下面這篇文章主要給大家介紹了關(guān)于Python中axis=0與axis=1指的方向有什么不同的相關(guān)資料,需要的朋友可以參考下2024-01-01
Python+OpenCV目標(biāo)跟蹤實現(xiàn)基本的運(yùn)動檢測
這篇文章主要為大家詳細(xì)介紹了Python+OpenCV目標(biāo)跟蹤實現(xiàn)基本的運(yùn)動檢測,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07

