Python(12)時間和日期(不完整)

2021-08-20 11:45:41 字數 1015 閱讀 2820

#coding=gbk

#時間和日期

import time

print(time.localtime())

#輸出time.struct_time(tm_year=2018, tm_mon=6, tm_mday=4, tm_hour=16, tm_min=57, tm_sec=26,

#對應年月日,小時,分鐘,秒,2018-6-4 16:57:26

#tm_wday=0, tm_yday=155, tm_isdst=0

#tm_wday代表星期,取值【0:6】,0代表星期一。tm_yday當前天數,取值【1:366】。tm_isdst夏令時

print(time.asctime())# 獲取當前時間 mon jun 4 17:04:13 2018

time.sleep(1)#當前執行緒睡眠時間 單位為 s 秒

#測量程式執行時間

def test():

sum=0

for i in range(1,99999):

sum+=i

return sum

t1=time.monotonic() #單向時鐘

print(test())

t2=time.monotonic()

print(' 執行時間為' ,(t2-t1))

#日期格式化字串

# time.strftime("%y年 %m月 %d日 %h時 %m分 %s秒",time.localtime())

print(time.strftime('%h:%m:%s',time.localtime()))

print(time.strftime('%y-%m-%d %h:%m:%s %w-%z',time.localtime()))

#????unicodeencodeerror: 'locale' codec can't encode character '\u5e74' in position 2: illegal byte sequence

62 時間和日期

1.strtotime 函式 echo strtotime now 相當於將英文單詞now直接等於現在的日期和時間,並把這個日期時間轉化為unix時間戳。這個效果跟echo time 一樣。echo strtotime 1 seconds 相當於將現在的日期和時間加上了1秒,並把這個日期時間轉化為u...

php學習5 時間和日期

如果時間時區不對,使用時間是要先設定時區,使用date default timezone set 設定新時區 date default timezone set asia shanghai echo date y m d h i s filemtime files 關於date 函式 date 函式...

python各種時間 Python日期和時間

本篇文章幫大家學習python日期和時間,包含了python日期和時間使用方法 操作技巧 例項演示和注意事項,有一定的學習價值,大家可以用來參考。通常在資料科學中,我們需要基於時間值的分析。python可以優雅地處理各種格式的日期和時間。日期時間庫提供了必要的方法和函式來處理下列情況。日期時間表示 ...