.strftime |
您所在的位置:网站首页 › datestrftime › .strftime |
由于时间模块提供了各种与时间有关的功能。因此,有必要导入时间模块,否则将由于时间的定义而出错。时间模块中存在strftime(format [,t])。 time.strftime(format[, t])函数转换代表时间的tuprl或struct_timegmtime()或者localtime()到由format参数指定的字符串。 如果未提供t,则返回当前时间localtime()用来。格式必须是字符串。如果t中的任何字段超出允许范围,则引发ValueError。 注意: 0是时间元组中任何位置的合法论点;如果通常是非法的,则将该值强制为正确的值。 用法: time.srtftime(format[, t]) 参数: t-要格式化的时间(以秒为单位) format-这是字符串类型。即指令可以嵌入格式字符串中。 返回值:没有 格式字符串中可以嵌入许多指令,您可以在此处引用它们。 笔记: 当与strptime()函数一起使用时,仅当%I指令用于解析小时时,%p指令才会影响输出小时字段。 范围实际上是0到61。值60在表示leap秒的时间戳中有效,并且出于历史原因支持值61。 与strptime()函数一起使用时,仅当指定星期几和年份时,%U和%W仅用于计算。 下面是实现: # Program To show How can we use different derivatives # Multiple at a time and single at a time # importing the srtftime() and gmtime() # if not used the gm time, time changes # to the local time from time import gmtime, strftime # using simple format of showing time s = strftime("%a, %d %b %Y %H:%M:%S + 1010", gmtime()) print("Example 1:", s) print() # only chnge in this is the full names # and the representation s = strftime("%A, %D %B %Y %H:%M:%S + 0000", gmtime()) print("Example 2:", s) print() # this will show you the preferd date time format s = strftime("%c") print("Example 3:", s) print() # this will tell about the centuries s = strftime("%C") print("Example 4:", s) print() # MOTY:month of the year # DOTY:Day of the year # Simple representation # % n - new line s = strftime("%A, %D %B %Y, %r, %nMOTY:%m %nDOTY:% j") print("Example 5:", s) print() # % R - time in 24 hour notation s = strftime(" %R ") print("Example 6:", s) print() # % H - hour, using a 24-hour clock (00 to 23) in Example 1, 2, 3 # % I - hour, using a 12-hour clock (01 to 12) s = strftime("%a, %d %b %Y %I:%M:%S + 0000", gmtime()) print("Example 7:", s) print() # % T - current time, equal to % H:% M:% S s = strftime("%r, %T ", gmtime()) print("Example 8:", s) print() # % u an % U use (see difference) s = strftime("%r, %u, %U") print("Example 9:", s) print() # use of % V, % W, % w s = strftime("%r, %V, %W, %w") print("Example 10:", s) print() # use of % x, % X, % y, % Y s = strftime("%x, %X, %y, %Y") print("Example 11:", s) print() # use of % Z, % z s = strftime("%r, %z, %Z") print("Example 12:", s) 输出: Example 1:Tue, 25 Jun 2019 10:09:52 + 1010 Example 2:Tuesday, 06/25/19 June 2019 10:09:52 + 0000 Example 3:Tue Jun 25 10:09:52 2019 Example 4:20 Example 5:Tuesday, 06/25/19 June 2019, 10:09:52 AM, MOTY:06 DOTY:% j Example 6: 10:09 Example 7:Tue, 25 Jun 2019 10:09:52 + 0000 Example 8:10:09:52 AM, 10:09:52 Example 9:10:09:52 AM, 2, 25 Example 10:10:09:52 AM, 26, 25, 2 Example 11:06/25/19, 10:09:52, 19, 2019 Example 12:10:09:52 AM, +0000, UTC |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |