YouTip LogoYouTip

Att Time Strftime

# Python2.x Python time strftime() Method * * * ## Description The Python time strftime() function is used to format time, returning the local time as a readable string, with the format determined by the format parameter. ## Syntax The strftime() method syntax: time.strftime(format[, t]) ## Parameters * format -- The format string. * t -- The optional parameter t is a struct_time object. ## Return Value Returns the local time as a readable string. ## Explanation Time and date formatting symbols in Python: * %y Two-digit year representation (00-99) * %Y Four-digit year representation (0000-9999) * %m Month (01-12) * %d Day of the month (0-31) * %H 24-hour clock hour (0-23) * %I 12-hour clock hour (01-12) * %M Minute (00-59) * %S Second (00-59) * %a Local abbreviated weekday name * %A Local full weekday name * %b Local abbreviated month name * %B Local full month name * %c Local appropriate date and time representation * %j Day of the year (001-366) * %p Local equivalent of A.M. or P.M. * %U Week number of the year (00-53), with Sunday as the first day of the week * %w Weekday (0-6), with Sunday as the first day of the week * %W Week number of the year (00-53), with Monday as the first day of the week * %x Local appropriate date representation * %X Local appropriate time representation * %Z Current time zone name * %% A literal '%' character ## Examples The following examples demonstrate the usage of the strftime() function: ## Example #!/usr/bin/python # -*- coding: UTF-8 -*- # Import __future__ package for Python3.x print compatibility # This line can be removed if using Python3.x from __future__ import print_function from datetime import datetime now =datetime.now()# current date and time year = now.strftime("%Y") print("year:", year) month = now.strftime("%m") print("month:", month) day = now.strftime("%d") print("day:", day) time= now.strftime("%H:%M:%S") print("time:",time) date_time = now.strftime("%Y-%m-%d, %H:%M:%S") print("date and time:",date_time) The output of the above example is: year: 2020 month: 09 day: 25 time: 10:24:28 date and time: 2020-09-25, 10:24:28
← Att Time StrptimeAtt Time Sleep β†’