Table of Contents
1. Time representation format
2. Time reading and format conversion
1. Use the time.time() method to read into floating point number format
2. Use time.localtime() and time.gmtime() methods to read into formatted format
3. Use the time.ctime() and time.asctime() methods to read into a standard readable format
4. Use time.strftime() and time.strptime() to complete the conversion with the custom format
Supplement: python time acquisition and conversion: time module and datetime module
Home Backend Development Python Tutorial What are the reading and conversion methods of time format in Python?

What are the reading and conversion methods of time format in Python?

Jun 02, 2023 am 11:07 AM
python

    1. Time representation format

    In Python, there are four commonly used formats for representing time, namely floating point format and standard readable format. formats, formatted formats, and custom formats. (The name is my own, unofficial naming)

    (1) Floating point format

    Use a floating point number in float format to represent time, and its specific meaning is expressed as the time from the world standard epoch ( The number of seconds from January 1, 1970 to this time node.

    (2) Standard readable format

    The format is - "Day of the week, month, date, hour: minute: second, year", which is easy for people to read.

    (3) Formatting format (time.struct_time)

    Use multiple parameters to represent information such as year, month, hour, minute, and second to facilitate computer processing.

    (4) Custom format

    Organize the time and date information into a custom format string according to your own needs.

    # 浮点数格式
    <class &#39;float&#39;>
    1667321639.1330378
    # 标准可读格式
    <class &#39;str&#39;>
    Wed Nov  2 00:53:59 2022
    # 格式化格式
    <class &#39;time.struct_time&#39;>
    time.struct_time(tm_year=2022, tm_mon=11, tm_mday=2, tm_hour=0, tm_min=53, tm_sec=59, tm_wday=2, tm_yday=306, tm_isdst=0)
    # 自定义格式
    <class &#39;str&#39;>
    2022年11月01日 11:59:59
    Copy after login

    The conversion relationship between different formats is as follows:

    What are the reading and conversion methods of time format in Python?

    2. Time reading and format conversion

    1. Use the time.time() method to read into floating point number format

    The time.time() method is used to read the computer's internal time, and the output is in floating point number format.

    import time
    t1 = time.time()
    print(t1)
    Out:
    1667322679.7262034
    Copy after login

    2. Use time.localtime() and time.gmtime() methods to read into formatted format

    time.localtime() and time.gmtime() methods to read local time respectively Time and world time, output in formatted format.

    import time
    t1 = time.localtime()  # 本地时间
    t2 = time.gmtime()  # 世界时间
    print(t1)
    print(t2)
    Out:
    time.struct_time(tm_year=2022, tm_mon=11, tm_mday=2, tm_hour=1, tm_min=12, tm_sec=58, tm_wday=2, tm_yday=306, tm_isdst=0)
    time.struct_time(tm_year=2022, tm_mon=11, tm_mday=1, tm_hour=17, tm_min=12, tm_sec=58, tm_wday=1, tm_yday=305, tm_isdst=0)
    Copy after login

    3. Use the time.ctime() and time.asctime() methods to read into a standard readable format

    When the time.ctime() and time.asctime() input parameters are When empty, the current time can be read directly into a standard readable format.

    import time
    t1 = time.ctime()
    t2 = time.asctime()
    print(t1)
    print(t2)
    Out:
    Wed Nov  2 01:10:01 2022
    Wed Nov  2 01:10:01 2022
    Copy after login

    The time.ctime() method can convert the floating point number format into a standard readable format.

    The time.asctime() method can convert the formatted format into a standard readable format.

    import time
    t1 = time.ctime(time.time())  # 将浮点数格式转换为标准可读格式
    t2 = time.asctime(time.localtime())  # 将格式化格式转换为标准可读格式
    print(t1)
    print(t2)
    Out:
    Wed Nov  2 01:01:41 2022
    Wed Nov  2 01:01:41 2022
    Copy after login

    4. Use time.strftime() and time.strptime() to complete the conversion with the custom format

    time.strftime() can convert the formatted format into a custom format .

    import time
    t1 = time.localtime()  # 格式化格式
    s1 = time.strftime("%Y-%m-%d %H:%M:%S", t1)
    print(s1)
    Out:
    2022-11-02 01:21:28  # 自定义格式
    Copy after login

    Compared with time.strftime(), time.strptime() is used to convert custom formats into time objects.

    import time
    s1 = "2022年11月01日 11:59:59"  # 自定义格式
    t1 = time.strptime(s1, "%Y年%m月%d日 %H:%M:%S")
    print(t1)
    Out:
    time.struct_time(tm_year=2022, tm_mon=11, tm_mday=1, tm_hour=11, tm_min=59, tm_sec=59, tm_wday=1, tm_yday=305, tm_isdst=-1)
    Copy after login

    Supplement: python time acquisition and conversion: time module and datetime module

    1. Three kinds of time: UTC time, local time, epoch time
    2. Three kinds of time representation : timestamp, struct_time, format time

    3.time.time() returns timestamp; time.localtime() returns time tuple; time.strftime('%Y-%m-%d', time .localtime()) returns the formatted time
    time.strptime('December 08, 2018, 34:10:04', '%Y year %m month %d day %M hour %I minute %S second ') Parse the string and return the time tuple

    4. The datetime module is a further encapsulation of time, mainly including 5 classes: date, time, datetime, timedelta, tzinfo.

    5.date.today(),date.fromtimestamp(timestamp),d.weekday(),d.strftime(format)
    6.datetime.today(),datetime.now([tz ])
    7. The internal value of timedelta stores days, seconds, and microseconds. All other values ​​are converted to these three parameters.
    8.td.days(), td.seconds()

    What are the reading and conversion methods of time format in Python?

    The above is the detailed content of What are the reading and conversion methods of time format in Python?. For more information, please follow other related articles on the PHP Chinese website!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

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

    Hot Article

    Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Nordhold: Fusion System, Explained
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1666
    14
    PHP Tutorial
    1273
    29
    C# Tutorial
    1254
    24
    PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

    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.

    Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

    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.

    How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

    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.

    PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

    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 vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

    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.

    Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

    Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

    Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

    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.

    How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

    Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

    See all articles