Python中datetime常用時(shí)間處理方法
更新時(shí)間:2015年06月15日 09:32:58 投稿:hebedich
Python提供了多個(gè)內(nèi)置模塊用于操作日期時(shí)間,像calendar,time,datetime。今天我們主要來探討下datetime的使用方法,有需要的小伙伴可以參考下。
常用時(shí)間轉(zhuǎn)換及處理函數(shù):
import datetime # 獲取當(dāng)前時(shí)間 d1 = datetime.datetime.now() print d1 # 當(dāng)前時(shí)間加上半小時(shí) d2 = d1 + datetime.timedelta(hours=0.5) print d2 # 格式化字符串輸出 d3 = d2.strftime('%Y-%m-%d %H:%M:%S') print d3 # 將字符串轉(zhuǎn)化為時(shí)間類型 d4 = datetime.datetime.strptime(date,'%Y-%m-%d %H:%M:%S.%f') print d4
獲取本周和本月第一天的日期:
# -*- coding:utf-8 -*- import datetime def first_day_of_month(): ''' 獲取本月第一天 :return: ''' # now_date = datetime.datetime.now() # return (now_date + datetime.timedelta(days=-now_date.day + 1)).replace(hour=0, minute=0, second=0, # microsecond=0) return datetime.date.today() - datetime.timedelta(days=datetime.datetime.now().day - 1) def first_day_of_week(): ''' 獲取本周第一天 :return: ''' return datetime.date.today() - datetime.timedelta(days=datetime.date.today().weekday()) if __name__ == "__main__": this_week = first_day_of_week() last_week = this_week - datetime.timedelta(days=7) this_month = first_day_of_month() last_month = this_month - datetime.timedelta(days=(this_month - datetime.timedelta(days=1)).day) print this_week print last_week print this_month print last_month
#! /usr/bin/python # coding=utf-8 import datetime """ datetime的功能強(qiáng)大 能支持0001年到9999年 """ """ 當(dāng)前時(shí)間 返回的是一個(gè)datetime類型 now方法有個(gè)參數(shù)tz,設(shè)置時(shí)區(qū)類型。如果沒有和方法today的效果一樣 """ now = datetime.datetime.now() #UTC時(shí)間 datetime.datetime.utcnow() attrs = [ ("year","年"),('month',"月"),("day","日"),('hour',"小時(shí)"),( 'minute',"分"),( 'second',"秒"),( 'microsecond',"毫秒"),( 'min',"最小"),( 'max',"最大"), ] for k,v in attrs: "now.%s = %s #%s" % (k,getattr(now, k),v) """ 返回一個(gè)time結(jié)構(gòu) """ now.timetuple() """ 返回一個(gè)date類型 """ now.date() """ 返回一個(gè)time類型 """ now.time() """ 當(dāng)前星期幾。星期一是0,星期于是6 注意這里是方法,不是屬性哦。 """ now.weekday() """ 當(dāng)前星期幾。星期一是1,星期于是7 注意這里是方法,不是屬性哦。 """ now.isoweekday() """ 修改當(dāng)前時(shí)間。比如修改成當(dāng)月1號(hào) """ now.replace(day=1) past = datetime.datetime(2010,11,12,13,14,15,16) """ 進(jìn)行比較運(yùn)算 返回的是timedelta類型 """ now-past """ 轉(zhuǎn)成字符串 詳細(xì)規(guī)則見Time篇 """ strdatetime = now.strftime("%Y-%m-%d %H:%M:%S") """ 字符串生成datetime對(duì)象 """ datetime.datetime.strptime(strdatetime, "%Y-%m-%d %H:%M:%S")
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關(guān)文章
Python中的支持向量機(jī)SVM的使用(附實(shí)例代碼)
這篇文章主要介紹了Python中的支持向量機(jī)SVM的使用(附實(shí)例代碼),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06kNN算法python實(shí)現(xiàn)和簡單數(shù)字識(shí)別的方法
這篇文章主要介紹了kNN算法python實(shí)現(xiàn)和簡單數(shù)字識(shí)別的方法,詳細(xì)講述了kNN算法的優(yōu)缺點(diǎn)及原理,并給出了應(yīng)用實(shí)例,需要的朋友可以參考下2014-11-11Python 調(diào)用 Windows API COM 新法
Python中調(diào)用Win32API 通常都是使用 PyWin32或者ctypes。本文給大家介紹Python 調(diào)用 Windows API COM 新法,感興趣的朋友跟隨小編一起看看吧2019-08-08Python爬取數(shù)據(jù)并寫入MySQL數(shù)據(jù)庫的實(shí)例
今天小編就為大家分享一篇Python爬取數(shù)據(jù)并寫入MySQL數(shù)據(jù)庫的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06