详解Python中的strftime()方法的使用
strftime()方法转换成一个元组或struct_time表示时间所指定的格式参数所返回gmtime()或localtime()为一个字符串。
当t不设置,所返回当前时间使用localtime()方法。格式必须是字符串。异常ValueError被挂起,如果t在任何字段的允许范围之外。
语法
以下是strftime()方法的语法:
time.strftime(format[, t])
参数
- t -- 这是以秒为单位来进行格式化的时间。
- format -- 这是将用于格式化给定的时间的指令。下面的指令可以嵌入格式字符串:
指令
- %a - 简写的星期几
- %A - 完整的星期几
- %b - 缩写的月份名称
- %B - 完整的月份名称
- %c - 首选日期和时间表示
- %C - 世纪值(年份除以100,范围从00到99)
- %d - 该月的第几天(01?31)
- %D - 类似 %m/%d/%y
- %e - 该月的一天(1?31)
- %g - 类似于%G,但是没有世纪
- %G - 对应于ISO周数4位数的年份(参见%V)
- %h - 类似于 %b
- %H - 小时,使用24小时制(00?23)
- %I - 小时,使用12小时制(01?12)
- %j - 一年中的哪一天(001?366)
- %m - 月份(01?12)
- %M - 分钟
- %n - 换行符
- %p - 根据给定的时间值am或pm
- %r - 时间在上午和下午的符号:am/pm
- %R - time in 24 hour notation
- %S - 秒
- %t - 制表符
- %T - 当前时间,等于 %H:%M:%S
- %u - 工作日为数字(1到7),星期一= 1。警告:在Sun Solaris上周日=1
- %U - 当年的周数,第一个星期日作为第一周的第一天
- %V - 本年度ISO 8601的周数(01到53),其中,第1周是在本年度至少4天的第一个星期,星期一作为一周的第一天
- %W - 当年的周数,与第一个星期一作为第一周的第一天
- %w - 星期为一个小数,星期日=0
- %x - 没有时间的日期表示
- %X - 无日期首选的时间表示
- %y - 一年无世纪(范围从00到99)
- %Y - 今年,包括世纪
- %Z or %z - 时区或名称或缩写
- %% -文字%字符
返回值
此方法不返回任何值。
例子
下面的例子显示strftime()方法的使用。
#!/usr/bin/python import time t = (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))
当我们运行上面的程序,它会产生以下结果:
Feb 18 2009 00:03:38

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.
