Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Python arrow date-time Module

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "Python arrow date and time module example analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Python has a lot of time and date processing library, there are time, datetime, etc., although it provides a very complete date, time and time zone conversion functions, but too many methods, not easy to remember, and often require a variety of conversion operations, very tedious, such as time and timestamp conversion, formatting time string conversion and so on, almost every time to use to take a look at the tutorial documentation. So is there a date-time processing library that is more user-friendly? Next, let's take a look at the arrow date-time library.

Arrow is a lightweight Python library that specializes in dealing with time and date. It provides a reasonable and user-friendly way to create, manipulate, format, and convert dates, times and timestamps. It is relatively easy to create time zone aware date and time instances.

You can use pip install arrow for installation.

The use of arrow module to get arrow object

Arrow can flexibly convert time data in various formats into Arrow objects, as follows:

Import arrowprint (repr (arrow.Arrow (2021, 8, 23, 8)) print (repr (arrow.get (2021, 8, 23, 8, 40)) print (repr (arrow.get ('2021-08-23 09 arrow.Arrow) print (repr (arrow.get (' 2021.08.23')) print (repr (arrow.get ('23 Univer (2012), 'DD/YYYY/MM')

The implementation results are as follows:

All of the above methods can convert character data into arrow objects, which is very flexible. In addition, you can convert timestamps to arrow objects.

Print (repr (arrow.get (1629683393.6558669)

Get the current time

Utc_time = arrow.utcnow () local_time = arrow.now () print (utc_time) print (local_time)

The utc time and the local time are obtained through the utcnow () function and the now () function, respectively. Of course, we can also specify the time zone when we call now (), thus getting the time of the specified time zone, such as arrow.now ('US/Pacific').

Time form conversion

When using date time, we often need conversion operations, such as converting to a time string in a specified format, converting to a timestamp, and so on.

Convert to a time string

Now = arrow.now () print (now) print (now.format ()) print (now.format ("YYYY-MM-DD hh:mm:ss")) print (now.format ("YYYY-MM-DD"))

The implementation results are as follows:

When you see this, do you feel more humane and easier to remember than the datetime module's'% Y-%M-%D% hrig% mpura% s' format?

Convert to timestamp

You can use t.timestamp to convert arrow objects into timestamps.

Now.timestamp acquires data

After converting to Arrow object, we can easily get all kinds of time data we want, such as year, month, day, hour, minute, second, week and other attributes, such as:

Now = arrow.now () print (now.year) print (now.month) print (now.day) print (now.hour) print (now.minute) print (now.second) print (now.week) modification time

Sometimes when we get a time, we need to modify the time, such as changing the time zone, changing the time, etc., we can use the following ways to modify.

Now = arrow.now () print (now.format ("YYYY-MM-DD hh:mm:ss")) # 2021-08-23 10:11:04now_utc = now.to ("utc") print (now_utc.format ("YYYY-MM-DD hh:mm:ss")) # 2021-08-23 02:11:04now1 = now.replace (day=31 Hour=12) print (now1.format ("YYYY-MM-DD hh:mm:ss")) # 2021-08-31 12:11:04now2 = now.shift (months=-2) print (now2.format ("YYYY-MM-DD hh:mm:ss")) # 2021-06-23 10:11:04

We can use the to () method to switch the time zone, the replace () method to modify the time, and the shift () method to move forward and backward.

This is the end of the content of "Python arrow date and time module example analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report