python中的時間和時間格式轉換

2021-06-26 12:17:50 字數 1816 閱讀 9808

import time

#time.struct_time(tm_year=2012, tm_mon=9, tm_mday=15, tm_hour=15, tm_min=1, tm_sec=44, tm_wday=5, tm_yday=259, tm_isdst=0)

print time.localtime() #返回tuple

#2012-09-15 15:01:44

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

#2012-09-15 03pm 01:44 今天是當年第259天 當年第37周 星期6

print time.strftime("%y-%m-%d %i%p %m:%s 今天是當年第%j天 當年第%u周 星期%w",time.localtime())

#1347692504.41 [秒數]:double

print time.time()

#指令 含義

#

#%y 有世紀的年份,如2012

#%m 十進位制月份[01,12].

#%d 當月的第幾天 [01,31].

#%h 24進製的小時[00,23].

#%m 十進位制分鐘[00,59].

#%s 秒數[00,61]. 61是有閏秒的情況

##%w 十進位制的數字,代表週幾 ;0是週日,1是周一.. [0(sunday),6].

##%i 十二進位制的小時[01,12].

#%p 上午還是下午: am or pm. (1)

##%j 當年第幾天[001,366].

#%u 當年的第幾周[00,53] 0=新一年的第乙個星期一之前的所有天被認為是在0周【週日是每週第一天】

#%w 當年的第幾周[00,53] 0=新一年的第乙個星期一之前的所有天被認為是在0周【周一是每週第一天】

# #%y 無世紀的年份[00,99]. 如12年

2.格式轉換

# 時間格式time的方法:

# localtime(秒數)    # :秒數-->time.struct_time

# mktime(time.struct_time)    #:time.struct_time-->秒數

# strftime("格式串",time.struct_time)    #:time.struct_time -->"yyyy-mm-dd hh:mm:ss"

# strptime(tuple_日期,"格式串")     #:"yyyy-mm-dd hh:mm:ss"-->time.struct_time

# 1. 秒數 ->[tuple]-> 年月日串

birth_secds = 485749800

tup_birth = time.localtime(birth_secds)

format_birth = time.strftime("%y-%m-%d %h:%m:%s",tup_birth)

# 2. 年月日串 ->[tuple]-> 秒數

print format_birth#1985-05-24 10:30:00

format_birth = "1985-05-24 10:30:00"

tup_birth = time.strptime(format_birth, "%y-%m-%d %h:%m:%s");

birth_secds = time.mktime(tup_birth)

print birth_secds#485749800.0

python中的時間格式

處理資料時經常會遇到處理時間的問題,拿原始資料中儲存的str格式的日期篩選,結果不對 python中有三種時間格式 1 時間戳 int,float型別 import time 2 元組 struct time型別 內涵九個元素 3 datetime型別 如 2017 11 28 14 49 43.0...

整理 Python中時間格式

python 中時間一般儲存為三種格式 time datetime calender 三種格式本身又都接受或者是能夠轉化成long型的timestamp 時間戳 或者是string型format形式 因此我們一般都是用其中的內建函式將他們轉化成時間戳進行傳遞或者是轉化成相應的時間字串進行輸出 有三種...

Python中時間格式轉化

1 datetime型別轉換成gmt時間格式的字串 如 thu,19 feb 2009 16 00 07 gmt strftime 官方釋義 new string from datetime import datetime gmt format a,d b y h m s gmt 0800 cst ...