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

How to realize the conversion between date and time stamp in Python3

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

It is believed that many inexperienced people don't know what to do about how to realize the conversion between date and time stamp in Python3. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

The unix_time function is the time stamp from normal time to unix, and date_time is the time when unix time changes to normal, such as year, month, day, hour, minute and second:

Import time "" date to time stamp "" def unix_time (dt): # convert to a time array timeArray = time.strptime (dt) "% Y-%m-%d% H:%M:%S") # converted to timestamp timestamp = int (time.mktime (timeArray)) return timestamp "" timestamp to date "" def custom_time (timestamp): # converted to localtime time_local = time.localtime (timestamp) # to new time format (2016-05-05 20:28:54) dt = time.strftime (" Y-%m-%d H:%M:%S " Time_local) return dttime_now = '2019-02-28 10:23:29'unix_t = unix_time (time_now) custom_t = custom_time (unix_t) print (unix_t) # 1551320609print (custom_t) # 2019-02-28 10 10:23:29'unix_t 23 Vantage 29 # what if it is a custom time format conversion The ideas and methods are the same. The comparison is as follows: "" time is displayed in the specified format, such as year-month-day to year / month / day "" dt = "2020-10-10 22:20:20" # to array timeArray = time.strptime (dt, "% Y-%m-%d% H:%M:%S") # to other display format customTime = time.strftime ("% Y/%m/%d% H:%M:%S") TimeArray) print (customTime) # 22:20:20 on 2020-10-10 "time is displayed in the specified format, such as year / month / day to year-month-day"dt =" 2020-10-10 22:20:20 "timeArray = time.strptime (dt,"% Y/%m/%d% H:%M:%S ") customTime = time.strftime ("% Y-%m-%d% H:%M:%S " TimeArray) print (customTime) # 2020-10-10 22:20:20 finish reading the above content Have you mastered the method of converting date to time stamp in Python3? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Internet Technology

Wechat

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

12
Report