python模組之time模組

來源:互聯網
上載者:User

標籤:python模組   轉換   模組   時間   今天   mon   date   拼接   replace   

import time

#從1970年1月1號淩晨開始到現在的秒數,是因為這一年unix的第一個商業版本上市了,這個最常用
# print(time.time())

# 1491574950.2398355


#返回當前的系統時間,也可加參數,比如下面的第二個例子
# print(time.ctime())
# print(time.ctime(time.time()-86400))

# Fri Apr 7 22:24:17 2017
# Thu Apr 6 22:25:15 2017



#可以把年、月、日、小時、分鐘分別顯示出來,但是這裡顯示的時間是格林威治時間,用下面第二個例子就可以做時間的字串拼接
# print(time.gmtime())
# time_obj = time.gmtime()
# print(time_obj.tm_year,time_obj.tm_mon)

# time.struct_time(tm_year=2017, tm_mon=4, tm_mday=7, tm_hour=14, tm_min=25, tm_sec=59, tm_wday=4, tm_yday=97, tm_isdst=0)
# 2017 4


#上面顯示的格林威治時間,下面這裡是顯示本地時間
# print(time.localtime())

# time.struct_time(tm_year=2017, tm_mon=4, tm_mday=7, tm_hour=22, tm_min=31, tm_sec=42, tm_wday=4, tm_yday=97, tm_isdst=0)


#把一個時間對象轉換成時間戳記

# print(time.mktime(time_obj))
# 1491546920.0


#等待10s
# time.sleep(10)
# print(‘wait 10s‘)


#將時間對象轉換成指定的字串格式,參數有兩個,分別是格式和時間對象,你可以選擇你想要的時間格式,比如就要日期,或者就時間,或者都要

# print(time.strftime("%Y-%m-%d:%H:%M:%S",time.gmtime()))
# 2017-04-07:14:40:28

# print(time.strftime("%Y-%m-%d:%H:%M:%S",time.localtime()))
# 2017-04-07:22:40:50


# ret = time.strptime(‘2016-12-23 15:34:34‘,‘%Y-%m-%d %H:%M:%S‘)
# print(ret)

# time.struct_time(tm_year=2016, tm_mon=12, tm_mday=23, tm_hour=15, tm_min=34, tm_sec=34, tm_wday=4, tm_yday=358, tm_isdst=-1)


import datetime

#顯示今天的日期
# print(datetime.date.today())
# 2017-04-07


# ret = datetime.datetime.now()
# print(ret)

# 2017-04-07 22:48:18.861383

#轉換time的形式
# print(ret.timetuple())

# time.struct_time(tm_year=2017, tm_mon=4, tm_mday=7, tm_hour=22, tm_min=49, tm_sec=46, tm_wday=4, tm_yday=97, tm_isdst=-1)

#下面介紹下時間的加減

#給當前的時間加10天
# print(datetime.datetime.now() + datetime.timedelta(days=10))
# 2017-04-17 22:55:03.070503


#給目前時間減10天
# print(datetime.datetime.now() + datetime.timedelta(days=-10))
# print(datetime.datetime.now() - datetime.timedelta(days=10))

# 2017-03-28 22:56:08.529247
# 2017-03-28 22:56:08.529247

#上面的例子是加減天,其實timedelta可以支援加減下面的參數,比如周,小時,分鐘
# days=0, seconds=0, microseconds=0,milliseconds=0, minutes=0, hours=0, weeks=0):


#
current_time = datetime.datetime.now()
print(current_time)

#替換為某年,某月,某日
print(current_time.replace(2016,1,3))
print(current_time.replace(2016,3))
print(current_time.replace(2015))

# 2016-01-03 23:01:54.095012
# 2016-03-07 23:01:54.095012
# 2015-04-07 23:01:54.095012



#兩個時間還可以做比較
# gtime = datetime.datetime.now()
# ctime = gtime.replace(2015)
# if gtime > ctime:
# print(‘true‘)
# else:
# print(‘false‘)

python模組之time模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.