Explain the use of the Strftime () method in Python

Source: Internet
Author: User
The Strftime () method is converted to a tuple or struct_time representing the time specified by the format parameter returned by Gmtime () or localtime () as a string.

When T is not 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 range of any field.
Grammar

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

Time.strftime (format[, T])

Parameters

    • T-this is the time in seconds for formatting.
    • Format-This is the instruction that will be used to format the given time. The following instruction can embed a format string:

Instructions

  • %a-Abbreviated day of the week
  • %A-The full day of the week
  • %B-Abbreviated month name
  • %B-Full month name
  • %c-Preferred date and time representation
  • %c-century value (year divided by 100, range from 00 to 99)
  • %d-the day ordinal 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-a year that corresponds to a 4-digit number of ISO weeks (see%V)
  • %h-Similar to%b
  • %H-hours, using 24-hour (00?23)
  • %I-hours, using 12-hour (01?12)
  • %j-which day of the year (001?366)
  • %m-month (01?12)
  • %M-min
  • %n-line break
  • %p-AM or PM based on a given time value
  • %r-time in the morning and the afternoon sign: am/pm
  • %r-time in hour notation
  • %s-Sec
  • %t-Tab
  • %T-current time, equals%h:%m:%s
  • %u-Weekday is a number (1 to 7), Monday = 1. Warning: on Sun Solaris Sunday =1
  • %u-the number of weeks of the year, the first Sunday as the first day of the first week
  • %V-The week of the year ISO 8601 (01 to 53), where the 1th week is at least 4 days in the year of the first week, Monday as the first day of the week
  • %W-the number of weeks of the year, with the first day of the first week of Monday as the first
  • %w-week for a decimal, Sunday =0
  • %x-date representation with no time
  • %x-No date preferred time representation
  • %y-One year without century (range from 00 to 99)
  • %Y-this year, including century
  • %Z or%Z-time zone or name or abbreviation
  • Percent-Text% character

return value

This method does not return any values.
Example

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

#!/usr/bin/pythonimport Timet = (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:

Feb 18 2009 00:03:38
  • 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.