current position:Home>How does Python represent time?2 modules, 3 ways, 1 text to get it~
How does Python represent time?2 modules, 3 ways, 1 text to get it~
2022-08-06 12:32:13【The programmer late maple】
大家好,这里是程序员晚枫.知识星球:Python读者圈
在Python中,表示时间的格式一共有3种:时间戳、结构化时间、格式化时间,2个模块:time、datetime.
今天我们来一起看一下.
一、3种时间格式,4种生成方式
1、时间戳 - 记录时间
时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.
import time
time.time() # 时间戳
# 输出:1659682465.1875775
复制代码
这种方式得到的时间,一般用来数据库存储,非常节省存储空间.
2、结构化的时间 - 使用时间
所谓结构化时间,你可以理解成把时间进行了分类,分为了:年月日时分秒,你想用哪个类别,就可以直接取出哪个类别.
如果我们想取出一个时间片段,用这个方法就很简单了,例如:取出当前时间的分钟数.
import time
time.localtime().tm_min # 结构化时间
# 输出:30
复制代码
3、格式化的时间 - 展示时间
这个方法,用来给用户展示时间.
import time
time.strftime('%Y-%m-%d %H-%M-%S %A')# 格式化时间
# 输出:'2022-08-04 19-08-35 Friday'
import datetime
datetime.datetime.now()#格式化时间
# 输出:datetime.datetime(2022, 8, 4, 19, 9, 0, 328515)
复制代码
二、2个模块:time,为什么有datetime模块?
在前面生成时间的代码中,我们使用了2个模块:time和datetime,好像它们之间的功能也是重复的.
既然有了time模块,为什么还要有datetime?那是为了简化time的使用.
datatime模块重新封装了time模块,提供更多接口,提供的类有:date,time,datetime,timedelta,tzinfo.
应用场景
在之前的文章中我们讲过:万字总结!Python 实现定时任务的八种方案
在定时任务中,我想设置一个一周后提醒我的功能.
如果用time模块进行实现
import time
time.time() + 7*24*60*60 # 7天*24小时*60分钟*60秒
复制代码
需要自己计算出7天后的时间戳,而如果使用datetime模块,这件事就很简单了:直接days + 7
,如下图代码所示.
import datetime
datetime.datetime.now() + datetime.timedelta(days=7)#格式化时间
复制代码
三、互相之间的转换
时间戳和结构化数据、字符串数据之间,可以进行转换.这一点的注意事项见上图,这里不再赘述.
如对本篇文章有疑问,可以在评论区,进行更加详细的沟通
copyright notice
author[The programmer late maple],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/218/202208061228143216.html
The sidebar is recommended
- Python's common modules of threading and Thread modules The first stage of thread implementation
- Blender Python Programming: Creating Emitting Materials
- Python multiprocessing
- How does python implement image edge detection
- Django paging method
- django regex
- How does Python represent time?2 modules, 3 ways, 1 text to understand~
- Modify column name and row index in pandas DataFrame
- [python pandas groupby]
- Python Daily Practice (New Question Bank of Niu Ke) - Day 20: Dictionary Practice
guess what you like
[LeetCode brush questions] Hash table - some questions are implemented with built-in functions (with Python code)
[LeetCode brush questions] Linked list topic (1) (with Python code)
[Small case of python learning] Simulation system invasion to enhance interest
Getting Started with Python Basics - Essential Knowledge for Getting Started
How does Python represent time?2 modules, 3 ways, 1 text to get it~
Python office software automation, master openpyxl operation in 5 minutes
Introduction to Python Basics - Variables, Strings
[python2] remove the u in front of the unicode string
How to use the Python Color class to draw text
How to use Asyncio Python web programming
Random recommended
- 27 Python artificial intelligence libraries have been sorted out, it is recommended to collect!
- [Python] Word2Vec predicts what will be left if I subtract 'love' from my 'life'
- When I export a pandas package, there is a problem. If I don't import it, there is no problem. Is this not enough memory?
- Python version 3.7.4 How can I install locust successfully?
- How does python use pyinstaller to package music into exe, that is, play background music when running the packaged program?
- Python use pyinstaller how to wrap up music exe, is to run a packaged program play background music?
- Rescue 007 of graph traversal application, detailed analysis of python version
- 27 Python artificial intelligence libraries have been sorted out, it is recommended to collect~
- pandas DataFrame data filtering (2)
- Python is how to represent time?- two modules, three ways, complete the ~ 1
- The definition of pycharm writing python code
- Problems defining functions in Python
- Python Socket Network Programming
- Django server running error
- Python image processing notes - image matching based on Harris corners (skimage)
- (Thirteen--1) Concurrent programming of python (thread, queue, process, coroutine, process thread coroutine comparison)
- (12) Python's memory management mechanism
- Python crawler entry case day07: Hippopx
- Django reports an error ModuleNotFoundError: No module named 'mysqlclient'
- Python study notes
- How to upgrade python? python3 version and install pip3
- [Python] 4-word summary for amateurs, PyCharm2022.2 Professional Edition improves development efficiency, basic settings and common shortcut keys (super practical) & basic knowledge of Python - CSDN 21-day learning challenge
- [Python Data Science Quick Start Series | 03] Playing with Data Extraction: Numpy Indexing and Slicing
- Python draws a curve experiment with matplotlib
- How to represent unequally spaced values with equally spaced values in Python?
- The principle of python decorator
- Python dictionary usage summary
- How to install python, configure environment variables, and change sources for third-party libraries
- [Conda] python data analysis/deep learning environment configuration windows+conda+jupyter+pytorch
- 【Conda】python environment setup , windows+conda+jupyter+pytorch (English Version)
- [Pandas] A primer on Pandas processing csv file datasets (neural network/machine learning algorithm data preprocessing)
- 100 days proficient in python (basic version) -- the first day: python and vscode environment installation
- How does python solve high concurrency
- Using the python library geopy method calculating the distance between the multiple sets of longitude and latitude
- JavaScript, Python 8x, 29x slower than C++?
- python---SSH connection to linux service
- [reprint] Python - list comprehension
- Python Computer Vision Programming - Chapter 2 Local Image Descriptors
- "Compared to Excel, easy learning Python data analysis," reading notes -- -- -- -- -- - data operation
- Why is a function in the math module opened in python or in the IDE of pycharm, only the definition is not implemented?