Python removes the leading 0 of the date

Source: Internet
Author: User

today, when using Python to get a date, use the date.strftime ('%y.%m.%d. %h%i ') to format the obtained date if the month is less than 10, a leading 0 appears. gets the date format such as: 2017.03.24.0510 so there will be a 0 in front of the month 3, but at this point we do not want this 0, so Baidu a bit, found are using%-d, but in the Pycharm run when the direct collapse, so can only find his way, found that there is a way "Date.timetuple (): Returns the Time.struct_time object corresponding to the date;" This method returns a struct, as follows:

Time.struct_time (tm_year=2017, tm_mon=3, tm_mday=24, tm_hour=19, tm_min=37, tm_sec=3, tm_wday=4, tm_yday=83, tm_isdst= -1), the number in this struct does not contain its leading 0, so it can be spliced using its struct body. So The code is as follows :

Time = Datetime.datetime.now (). Timetuple ()
Versioninfo = str (time.tm_year) + '. ' + str (time.tm_mon) + '. ' + str (time.tm_mday) + '. ' + str (time.tm_hour) + str (time.tm _min)

Datetime.datetime.now (). Timetuple () returns a struct in which the corresponding data can be obtained by means of the method tm_xxx, which is only used for stitching.
The value of the obtained versioninfo is: ' 2017.3.24.1937 ', which is exactly what I want. Haha, it's done.

Python removes the leading 0 of the date

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.