Explain the use of the Strftime () method in Python

Source: Internet
Author: User
Tags current time iso 8601 month name in python


This article mainly introduces the use of the Strftime () method in Python, which is the basic knowledge in Python's introductory study, and can be consulted by a friend.



The Strftime () method converts to a tuple or struct_time a string that returns Gmtime () or localtime () for the format parameter specified by the time.



When T is set, the current time is returned using the LocalTime () method. The format must be a string. The exception valueerror is suspended if T is outside the allowable bounds of any field.



Grammar



The following is the syntax for the Strftime () method:




Time.strftime (format[, T])


Parameters



T-this is the time to format in seconds.



Format-This is the instruction that will be used to format the given time. The following instructions can be embedded in a format string:



Instructions



%a-Day of the week



%A-Full day of the week



%B-Abbreviated month names



%B-Full month name



%c-Preferred date and time representation



%c-century value (year divided by 100, range from 00 to 99)



%d-day of the month (01?31)



%d-Similar%m/%d/%y



%e-Day of the month (1?31)



%g-similar to%g, but no century



%G-year of 4 digits corresponding to ISO week (see%V)



%h-Similar to%b



%H-hours, using a 24-hour system (00?23)



%I-hours, using a 12-hour system (01?12)



%j-which day of the year (001?366)



%m-month (01?12)



%m-min



%n-line Feed



%p-based on the given time value am or PM



%r-time in the morning and afternoon symbols: am/pm



%r-time in hour notation



%s-Sec



%t-Tab



%T-current time, equal to%h:%m:%s



%u-workdays are numbers (1 to 7), Monday = 1. Warning: on Sun Solaris, Sunday =1



%u-Number of weeks of the year, first Sunday as the first day of the first week



%V-this year's ISO 8601 weeks (01 to 53), of which the 1th week is at least 4 days of the first week of the year, Monday as the first day of the week



%w-the number of weeks of the year, with the first Monday as the first day of the first week



%w-week for a decimal, Sunday =0



%x-No date representation of time



%x-No date preferred time representation



%y-One year without century (range from 00 to 99)



%Y-this year, including the century



%Z or%Z-time zone or name or abbreviation



Percent%-literal% character



return value



This method does not return any values.



Example



The following example shows the use of the Strftime () method.




#!/usr/bin/python Import Time t = (2009, 2, 3, 1, 0) t = time.mktime (t) print time.strftime ("%b%d%Y%H :%m:%s ", Time.gmtime (t))


When we run the above program, it produces the following results:


Related Article

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.