Explain how to use strftime () in Python

Source: Internet
Author: User
Tags iso 8601
This article describes how to use the strftime () method in Python. it is the basic knowledge of getting started with Python. For more information, see strftime () the method is converted into a tuples or struct_time, which indicates that the gmtime () or localtime () returned by the format parameter specified by the time is a string.

If t is not set, the current time returned uses the localtime () method. The format must be a string. Exception ValueError is suspended if t is out of the permitted range of any field.
Syntax

The syntax of the strftime () method is as follows:

time.strftime(format[, t])

Parameters

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

Command

  • % A-abbreviated day of the week
  • % A-complete day of the week
  • % B-abbreviated month name
  • % B-complete month name
  • % C-expression of the preferred date and time
  • % C-century value (year divided by 100, range: 00 to 99)
  • % D-the day of the month (01? 31)
  • % D-similar to % m/% d/% y
  • % E-the day of the month (1? 31)
  • % G-similar to % G, but not Century
  • % G-year corresponding to the four-digit ISO week (see % V)
  • % H-similar to % B
  • % H-Hour, in 24-hour format (00? 23)
  • % I-hour, in 12-hour format (01? 12)
  • % J-which day of the year (001? 366)
  • % M-month (01? 12)
  • % M-min
  • % N-linefeed
  • % P-according to the given time value am or pm
  • % R-symbol of the time in the morning and afternoon: am/pm
  • % R-time in 24 hour notation
  • % S-second
  • % T-tab
  • % T-current time, equal to % H: % M: % S
  • % U-workday is a number (1 to 7), Monday = 1. Warning Sunday = 1 on Sun Solaris
  • % U-The number of weeks in the current year. The first Sunday is the first day of the first week.
  • % V-The number of weeks of ISO 8601 of the current year (01 to 53), of which week 1st is the first week of at least four days of the current year, and Monday is the first day of the week.
  • % W-the week number of the current year, and the first Monday as the first day of the first week
  • % W-week is a decimal number, Sunday = 0
  • % X-date representation without time
  • % X-time representation with no date preference
  • % Y-century-free year (range: 00 to 99)
  • % Y-this year, including Century
  • % Z or % z-time zone or name or abbreviation
  • %-Text % characters

Return value

This method does not return any value.
Example

The following example shows how to use the strftime () method.

#!/usr/bin/pythonimport timet = (2009, 2, 17, 17, 3, 38, 1, 48, 0)t = time.mktime(t)print time.strftime("%b %d %Y %H:%M:%S", time.gmtime(t))

When we run the above program, it will produce 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.