python 常用 time, datetime處理,pythondatetime

來源:互聯網
上載者:User

python 常用 time, datetime處理,pythondatetime

python 中 time 有三種格式:

float,

struct tuple(time.struct_time 或 datetime.datetime),

str

 

常用的:

float --> struct tuple:   time.localtime( float )

struct time tuple --> str: time.strftime(format, struct time tuple)

str --> struct time tuple: time.strptime(str, format)

struct time tuple --> float : time.mktime(struct time tuple)

struct time tuple --> datetime: datetime(*time_tuple[0:6])

 

float --> datetime: datetime.datetime.fromtimestamp( float )

datetime --> str: datetime.strftime(format, datetime)

str --> datetime: datetime.strptime(str, format)

datetime --> struct time tuple: datetime.timetuple()

 

Note:

time 是 float 為基礎,小數點後是毫秒,整數部分是秒。(Java 是毫秒,所以,python_time*1000 == Java_time)

datetime 是int, 略去了毫秒部分。datetime tuple 少於 struct_time

 

1. 目前時間

>>> import time>>> time.time()1450681042.751>>> time.localtime(time.time())time.struct_time(tm_year=2015, tm_mon=12, tm_mday=21, tm_hour=15, tm_min=0, tm_sec=2, tm_wday=0, tm_yday=355, tm_isdst=0)>>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))'2015-12-21 15:01:28'>>> from datetime import datetime>>> datetime.now()datetime.datetime(2015, 12, 21, 14, 58, 38, 279000)>>> datetime.today()datetime.datetime(2015, 12, 21, 14, 59, 20, 204000)>>> now = datetime.now()>>> now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond>>> now.isocalendar() # 2015年 第52周 星期一(2015, 52, 1)>>> now.isoweekday() # 星期幾,1:Monday; 而 now.weekday() 傳回值從0開始1

 

2. 日期文字--> 日期

>>> s='2015-12-21 15:01:28'>>> timeTuple = datetime.strptime(s, '%Y-%m-%d %H:%M:%S')datetime.datetime(2015, 12, 21, 15, 1, 28)>>> datetime.datetime.strftime('%Y/%m/%d %H:%M:%S', timeTuple)

>>> s='2015-12-21 15:01:28'>>> timeTuple = datetime.strptime(s, '%Y-%m-%d %H:%M:%S')time.struct_time(tm_year=2015, tm_mon=12, tm_mday=21, tm_hour=15, tm_min=1, tm_sec=28, tm_wday=0, tm_yday=355, tm_isdst=-1)>>> time.strftime('%Y/%m/%d %H:%M:%S',timeTuple)
  python中時間日期格式化符號:  %y 兩位元的年份表示(00-99)  %Y 四位元的年份表示(000-9999)  %m 月份(01-12)  %d 月內中的一天(0-31)  %H 24小時制小時數(0-23)  %I 12小時制小時數(01-12)   %M 分鐘數(00=59)  %S 秒(00-59)    %a 本地簡化星期名稱  %A 本地完整星期名稱  %b 本地簡化的月份名稱  %B 本地完整的月份名稱  %c 本地相應的日期表示和時間表示  %j 年內的一天(001-366)  %p 本地A.M.或P.M.的等價符  %U 一年中的星期數(00-53)星期天為星期的開始  %w 星期(0-6),星期天為星期的開始  %W 一年中的星期數(00-53)星期一為星期的開始  %x 本地相應的日期表示  %X 本地相應的時間表示  %Z 當前時區的名稱  %% %號本身 

 

3. 時間戳記

>>> time.mktime(time.strptime(s,'%Y-%m-%d %H:%M:%S'))1450681288.0>>> int(time.time())
# timestamp to time tuple in UTCtimestamp = 1226527167.595983time_tuple = time.gmtime(timestamp)print repr(time_tuple)
# timestamp to time tuple in local timetimestamp = 1226527167.595983time_tuple = time.localtime(timestamp)print repr(time_tuple)

 

2. 日期相加減

import datetimenow = datetime.datetime.now() # datetime.datetime(2015, 12, 16, 15, 6, 37, 420000)dayOfweek = datetime.datetime.isoweekday()if dayOfweek == 1: # Monday    last_time = now + datetime.timedelta(days=-3)else:    last_time = now + datetime.timedelta(days=-1)


 轉:http://www.cnblogs.com/snow-backup/p/5063665.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.