当前位置:网站首页>Time module: acquisition and mutual transformation of timestamp, structured time and formatted time
Time module: acquisition and mutual transformation of timestamp, structured time and formatted time
2022-07-27 02:54:00 【Fried eggplant】
Catalog
02 Get the current structured time
03 Get the current format time
04 Mind map of mutual transformation
05 The mutual transformation of timestamp and structured time
06 The mutual transformation of structured time and formatted time
07 Understanding knowledge points
01 Get the current timestamp
Time stamp : Greenwich Time 1970 year 01 month 01 Japan 00 when 00 branch 00 second ( Beijing Time 1970 year 01 month 01 Japan 08 when 00 branch 00 second ) The total number of seconds since
import time
timestamp = time.time()
02 Get the current structured time
import time
struct_time = time.localtime()
03 Get the current format time
Method 1
import time
format_time = time.strftime('%Y-%m-%d %H:%M:%S %p')
# %Y、%m、%d... Is a special character , Indicate year 、 month 、 Japan ...
This is the official document explanation , Translation in brackets
%Y Year with century as a decimal number.( year ) %m Month as a decimal number [01,12].( month ) %d Day of the month as a decimal number [01,31].( Japan ) %H Hour (24-hour clock) as a decimal number [00,23].( when -24 when ) %M Minute as a decimal number [00,59].( branch ) %S Second as a decimal number [00,61].( second ) %z Time zone offset from UTC.( International standard time and local time ( East eight district Beijing time ) The time difference ) %a Locale's abbreviated weekday name.( What day - Abbreviation ) %A Locale's full weekday name.( What day - Full name ) %b Locale's abbreviated month name.( The name of the month - Abbreviation ) %B Locale's full month name.( The name of the month - Full name ) %c Locale's appropriate date and time representation.(linux Time format of ) %I Hour (12-hour clock) as a decimal number [01,12].( when -12 when ) %p Locale's equivalent of either AM or PM.( In the morning or Afternoon )(%x --> when / branch / second )
The above is also acceptable :
import time
format_time = time.strftime('%Y-%m-%d %X %p')
# namely %X Equivalent to %H:%M:%S The format of Method 2 ( Format cannot be customized )
import datetime
format_time = datetime.datetime.now() # Get local time ( East eight time )
# understand : Get international standard time
format_utc_time = datetime.datetime.utctime()
The format is constant : %Y-%m-%d %H:%M:%S
04 Mind map of mutual transformation
Maybe it will be helpful for the next time format conversion

05 The mutual transformation of timestamp and structured time
Time stamp (timestamp) --> Structured time (struct_time)====>time.localtime(timestamp)
import time
timestamp = 3333333 # Let me give you a random example
struct_time = time.localtime(timestamp) # If there is no reference , The default incoming time.time() The current timestamp
Structured time (struct_time --> Time stamp (timestamp)====>time.mktime(struct_time)
import time
struct_time = time.localtime() # Take the current structured time as an example
timestamp = time.mktime(struct_time) # Incoming structured time
06 The mutual transformation of structured time and formatted time
Structured time (struct_time) --> Format time (format_time)====>time.strftime(format, struct_time)
import time
struct_time = time.localtime() # Take the current structured time as an example
format_time = time.strftime('%Y-%m-%d %H:%M:%S', struct_time)
# Parameters 1 The format is determined by yourself , Parameter 2 is passed into the structured time , If not, the current structured time will be defaulted
Format time (format_time) --> Structured time (struct_time)====>time.strptime(format_time, format)
import time
format_time = time.strftime('%Y-%m-%d') # Suppose, for example, the format time of such a format
struct_time = time.strptime(format_time, '%Y-%m-%d')
# Parameters 1 Given formatting time , Parameters 2 Given the format of the previous format time 07 Understanding knowledge points
The timestamp is directly converted to the formatted time ( The format is fixed to %Y-%m-%d %H:%M:%S Can't customize )
import datetime
import time
timestamp = time.time()
format_time = datetime.datetime.fromtimestamp(timestamp) # Incoming timestamp

边栏推荐
- 消息队列学习 -- 概念
- It has been established for 3 years, and now goose factory has an annual income of millions +. As some suggestions of software testing predecessors
- Lua函数之非全局函数
- c语言:深度学习递归
- OD-Paper【3】:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
- I heard that you knelt on the interface test during the interview?
- C language: deep learning recursion
- 测试人需要的数据库知识:MySQL常用语法
- LeetCode->二分查找打卡(三)
- C语言程序的编译上
猜你喜欢

How many holes have you stepped on in BigDecimal?

面试突击68:为什么 TCP 需要 3 次握手?

Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round

Greed - 376. Swing sequence

Plato farm is expected to further expand its ecosystem through elephant swap

com.fasterxml.jackson.databind.exc.InvalidDefinitionException

uni-app 微信小程序搜索关键字标红显示

com.fasterxml.jackson.databind.exc.InvalidDefinitionException

C语言程序的编译上

OD-Paper【3】:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
随机推荐
Uni app wechat applet search keywords are displayed in red
什么是进程?
pyqt5使用pyqtgraph画动态散点图
想要彻底搞的性能优化,得先从底层逻辑开始了解~
多线程的具体使用
JS 数组去重(含简单数组去重、对象数组去重)
Use of formdata
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
Which securities firm is safer to open an account and buy REITs funds?
time模块: 时间戳、结构化时间、格式化时间的获取与相互转化
人们为什么热衷于给事物排序
消息队列学习 -- 概念
C language program compilation
The latest multi-threaded & highly concurrent learning materials, interview confidence
Debezium系列之:基于debezium offset拉取历史数据,确保数据没有丢失
After working in Tencent testing post for 5 years, I was ruthlessly dismissed in July, trying to wake up my brother who was still paddling
Tabbar of customized wechat applet on uni app
uni-app上自定义微信小程序的tabbar
C语言程序的编译(预处理)下
快速排序(Quick sort)