Python time模組詳解(時間戳↔元組形式↔格式化形式三者轉化)
時間表示三種形式
在Python中, time有三種表示形式
1 時間戳:1970年1月1日之後的秒
2 元組格式化形式 包含了:年、日、星期 得到time.struct_time(
tm_year=2017, tm_mon=10, tm_mday=14…)
print(‘2.元組格式化形式:{}’.format(time.gmtime()))
3 視覺化的字串 2017-11-11 11:44
```#!usr/bin/env python#_*_ coding:utf-8 _*_# time有三種表示形式import time#1 時間戳:1970年1月1日之後的秒print('1.時間戳形式:{}'.format(time.time()))#2 元組格式化形式 包含了:年、日、星期 得到time.struct_time(tm_year=2017, tm_mon=10, tm_mday=14...)print('2.元組格式化形式:{}'.format(time.gmtime()))#3 視覺化的字串 2017-11-11 11:44print('3.視覺化的字串:{}'.format(time.strftime('%F %H:%M:%S ''''
time.strftime()可以用來獲得當前時間,可以將時間格式化為字串等等
格式命令列在下面:(區分大小寫)
%a 星期幾的簡寫
%A 星期幾的全稱
%b 月分的簡寫
%B 月份的全稱
%c 標準的日期的時間串
%C 年份的後兩位數字
%d 十進位制表示的每月的第幾天
%D 月/天/年
%e 在兩字元域中,十進位制表示的每月的第幾天
%F 年-月-日
%g 年份的後兩位數字,使用基於周的年
%G 年分,使用基於周的年
%h 簡寫的月份名
%H 24小時制的小時
%I 12小時制的小時
%j 十進位制表示的每年的第幾天
%m 十進位制表示的月份
%M 十時製表示的分鐘數
%n 新行符
%p 本地的AM或PM的等價顯示
%r 12小時的時間
%R 顯示小時和分鐘:hh:mm
%S 十進位制的秒數
%t 水平製表符
%T 顯示時分秒:hh:mm:ss
%u 每週的第幾天,星期一為第一天 (值從0到6,星期一為0)
%U 第年的第幾周,把星期日做為第一天(值從0到53)
%V 每年的第幾周,使用基於周的年
%w 十進位制表示的星期幾(值從0到6,星期天為0)
%W 每年的第幾周,把星期一做為第一天(值從0到53)
%x 標準的日期串
%X 標準的時間串
%y 不帶世紀的十進位制年份(值從0到99)
%Y 帶世紀部分的十制年份
%z,%Z 時區名稱,如果不能得到時區名稱則返回空字元。
%% 百 時間轉化time.localtime()方法
#用time.localtime()方法,將一個時間戳轉換為當前時區的struct_time。print('4.時間戳轉化元組形式時間time.localtime')print(time.localtime(time.time()))#5 轉化為strftime(format[, tuple]) -> string#將指定的struct_time(預設為當前時間),根據指定的格式化字串輸出print('5.指定的格式化字串轉化:time.strftime(format[, tuple]-> string')print(time.strftime('%Y-%m-%d',time.localtime(time.time())))#6 time.asctime([t])把一個表示時間的元組或者struct_time表示為這種形式print('6.time.asctime([t])把一個表示時間的元組或者struct_time表示為這種形式')print(time.asctime())#7 time.ctime([secs]):把時間戳(按秒計算的浮點數)轉化為time.asctime()的形式。print('7 time.ctime([secs]):把時間戳(按秒計算的浮點數)轉化為time.asctime()的形式。')print(time.ctime(time.time()))#8 time.mktime將一個struct_time轉化為時間戳。print('8 time.mktime將一個struct_time轉化為時間戳。')print(time.mktime(time.localtime()))
時間戳計算時間差
根據時間戳來計算(注意時間戳時秒還是毫秒)
天數
printed(time.time()+86400*7) 當前時間的後7天
小時
printed(time.time()+3600*7) 當前時間的後7小時
分鐘
printed(time.time()+60*7) 當前時間的後7分鐘
字串時間差
start ="2018-06-19 17:37:31"end = "2019-07-30 17:37:31"start=time.strptime(start, "%Y-%m-%d %H:%M:%S")end=time.strptime(end, "%Y-%m-%d %H:%M:%S") userStart=datetime.datetime(start[0],start[1],start[2]) userEnd=datetime.datetime(end[0],end[1],end[2]) print ((userEnd-userStart).days) # 0 天數 # print (end-start).total_seconds() # 30.029522 精確秒數 # print (end-start).seconds # 30 秒數 # print (end-start).microseconds # 29522 毫秒數
datetime標準形式
diff=datetime.datetime.now() - datetime.timedelta(days=7)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2618/viewspace-2804017/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python之time模組詳解Python
- python時間模組time和datetimePython
- Python模組:time模組Python
- 格式化時間 戳
- c++時間形式轉換C++
- Python多維列表(元組)合併成一維形式Python
- Python時間處理常用模組及用法詳解!Python
- Python元組詳解Python
- python模組詳解Python
- python中sys,os,time模組的使用(包括時間格式的各種轉換)Python
- Python入門—time模組Python
- python 時間相關模組Python
- C++資料格式化4 - 格式化時間戳C++時間戳
- 時間戳轉化為時間格式時間戳
- Python的常見模組:OS和 time模組介紹Python
- 每週一個 Python 模組 | timePython
- day18:json模組&time模組&zipfile模組JSON
- Python中使用dateutil模組解析時間Python
- Python時間模組常用操作總結Python
- Python格式化時間Python
- time模組,collections模組,佇列和棧佇列
- python中的時間轉換,秒級時間戳轉string,string轉時間Python時間戳
- python列表(list)和元組(tuple)詳解Python
- 以太坊原始碼分析(1)go-ethereum的設計思路及模組組織形式原始碼Go
- Python 正規表示式模組詳解Python
- python之logging日誌模組詳解Python
- 如何將UTC時間轉換為Unix時間戳(How to convert UTC time to unix timestamp)時間戳
- matplotlib模組詳解
- difflib模組詳解
- psutil模組詳解
- 前端模組化詳解(完整版)前端
- python時間戳和時間字串的各種轉換Python時間戳字串
- 如何將Python時間戳轉換為時間?Python學習教程!Python時間戳
- C# 時間戳轉時間C#時間戳
- Python 列表、元組、字典及集合操作詳解Python
- Python常用模組(random隨機模組&json序列化模組)Pythonrandom隨機JSON
- Python3 pickle模組的使用詳解Python
- python3.x中argparse模組詳解Python