How to calculate time difference in python
How python calculates the time difference: first introduce the datetime package; then use the "(time_2_struct - time_1_struct)" method to calculate the time difference on the same day or the time difference on different days.
The operating environment of this article: Windows 7 system, python version 2.7.14, DELL G3 computer.
Python to find the time difference
Python to find the time difference mainly uses the datetime package, including the time difference under the same day and the time difference under different weather conditions.
from datetime import datetime, date
1. The time difference (seconds) under the same day situation, minutes can be divided by the number of seconds by 60
#计算时间差的分钟数 # 同一天的时间差 time_1 = '2020-03-02 15:00:00' time_2 = '2020-03-02 16:00:00' time_1_struct = datetime.strptime(time_1, "%Y-%m-%d %H:%M:%S") time_2_struct = datetime.strptime(time_2, "%Y-%m-%d %H:%M:%S") seconds = (time_2_struct - time_1_struct).seconds print('同一天的秒数为:') print(seconds)
2. Different days situation The time difference below (the time difference under the same day situation can also be calculated), total_seconds
# 不同天的时间差 time_1 = '2020-03-02 15:00:00' time_2 = '2020-03-03 16:00:00' time_1_struct = datetime.strptime(time_1, "%Y-%m-%d %H:%M:%S") time_2_struct = datetime.strptime(time_2, "%Y-%m-%d %H:%M:%S") # 来获取时间差中的秒数。注意,seconds获得的秒只是时间差中的小时、分钟和秒部分,没有包含天数差,total_seconds包含天数差 # 所以total_seconds两种情况都是可以用的 total_seconds = (time_2_struct - time_1_struct).total_seconds() print('不同天的秒数为:') print(int(total_seconds)) min_sub = total_seconds / 60 print('不同天的分钟数为:') print(int(min_sub))
[Recommended learning: python video tutorial】
3. When there is only time without date, to find the time difference, you can first add the same date, and then find the time difference, datetime.combine method
# 只有时间time没有日期时,求时间差先可以加上一个相同的日期,再求时间差 # date.min能表示的最小日期 # date.max能表示的最大日期 # date.today()返回一个当前日期对象 # datetime.combine:根据所给的date和time创建一个datetime对象 time_sub = datetime.combine(date.min, time_2_struct.time()) - datetime.combine(date.min, time_1_struct.time()) print('----- 与最小日期结合: ------') print(time_sub.seconds/60) time_sub = datetime.combine(date.today(), time_2_struct.time()) - datetime.combine(date.today(), time_1_struct.time()) print('----- 与当天日期结合: ------') print(time_sub.seconds/60) print(time_sub.total_seconds()/60)
##
The above is the detailed content of How to calculate time difference in python. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Golang and Python each have their own advantages: Golang is suitable for high performance and concurrent programming, while Python is suitable for data science and web development. Golang is known for its concurrency model and efficient performance, while Python is known for its concise syntax and rich library ecosystem.
