In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how Python outputs date and time". In daily operation, I believe many people have doubts about how Python outputs date and time. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "how to output date and time by Python". Next, please follow the editor to study!
Python programs can process dates and times in many ways, and converting date formats is a common feature.
Python provides a time and calendar module that can be used to format dates and times.
The interval is a floating-point decimal in seconds.
Each timestamp is expressed in terms of how long has elapsed since midnight on January 1, 1970.
There are many functions under Python's time module that can convert common date formats. For example, the function time.time () is used to obtain the current timestamp, as shown below:
#! / usr/bin/python#-*-coding: UTF-8-*-import time; # introduces time module ticks = time.time () print "current timestamp is:", ticks
The output result of the above example:
The current timestamp is: 1459994552.51
The timestamp unit is most suitable for date operation. But dates before 1970 cannot be expressed in this way. Not even if it's too far away. UNIX and Windows only support it until 2038.
Get the current time
Switch from returning a floating-point number to a time tuple by passing the floating-point number to a function such as localtime.
#! / usr/bin/python#-*-coding: UTF-8-*-import timelocaltime = time.localtime (time.time ()) print "Local time:", localtime
The output result of the above example:
Local time: time.struct_time (tm_year=2016, tm_mon=4, tm_mday=7, tm_hour=10, tm_min=3, tm_sec=27, tm_wday=3, tm_yday=98, tm_isdst=0)
Get the time of the format
You can choose a variety of formats according to your needs, but the easiest function to get readable time patterns is asctime ():
#! / usr/bin/python#-*-coding: UTF-8-*-import timelocaltime = time.asctime (time.localtime (time.time () print "Local time is:", localtime
The output result of the above example:
Local time: Thu Apr 7 10:05:21 2016
Format date
We can use the strftime method of the time module to format the date:
Time.strftime (format [, t]) #! / usr/bin/python#-*-coding: UTF-8-*-import time# formatted in the form 2016-03-20 11:45:39 print time.strftime ("% Y-%m-%d% H:%M:%S", time.localtime ()) # formatted into Sat Mar 28 22:24:24 2016 form print time.strftime ("a% b% d% H:%M:%S% Y" Time.localtime () # converts the format string to a timestamp a = "Sat Mar 28 22:24:24 2016" print time.mktime (time.strptime (a, "a b d H:%M:%S Y"))
The output result of the above example:
2016-04-07 10:25:09Thu Apr 07 10:25:09 20161459175064.0
Get a monthly calendar
The Calendar module has a wide range of methods for dealing with annual and monthly calendars, such as printing a monthly calendar for a month:
#! / usr/bin/python#-*-coding: UTF-8-*-import calendarcal = calendar.month (2016, 1) print "the following outputs the calendar for January 2016:" print cal
The output result of the above example:
The following output January 2016 calendar: January 2016Mo Tu We Th Fr Sa Su 12 3 4 5 6 7 8 9 1011 12 13 14 15 16 1718 19 20 21 22 23 2425 26 27 28 29 30 31 this is the end of the study on "how Python output date and time", I hope to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.