在Python中,日期和时间不是其自身的数据类型,而是名为

strftime()函数用于将日期和时间对象转换为其字符串表示形式。它需要一个或多个格式化代码输入,并返回字符串表示形式。

用法:

strftime(format)

返回值:它返回日期或时间对象的字符串表示形式。

格式代码列表:格式代码参考表。

指示

含义

输出格式

%a

Abbreviated weekday name.

Sun, Mon, …

%A

Full weekday name.

Sunday, Monday, …

%w

Weekday as a decimal number.

0, 1, …, 6

%d

Day of the month as a zero added decimal.

01, 02, …, 31

%-d

Day of the month as a decimal number.

1, 2, …, 30

%b

Abbreviated month name.

Jan, Feb, …, Dec

%B

Full month name.

January, February, …

%m

Month as a zero added decimal number.

01, 02, …, 12

%-m

Month as a decimal number.

1, 2, …, 12

%y

Year without century as a zero added decimal number.

00, 01, …, 99

%-y

Year without century as a decimal number.

0, 1, …, 99

%Y

Year with century as a decimal number.

2013, 2019 etc.

%H

Hour (24-hour clock) as a zero added decimal number.

00, 01, …, 23

%-H

Hour (24-hour clock) as a decimal number.

0, 1, …, 23

%I

Hour (12-hour clock) as a zero added decimal number.

01, 02, …, 12

%-I

Hour (12-hour clock) as a decimal number.

1, 2, … 12

%p

Locale’s AM or PM.

AM, PM

%M

Minute as a zero added decimal number.

00, 01, …, 59

%-M

Minute as a decimal number.

0, 1, …, 59

%S

Second as a zero added decimal number.

00, 01, …, 59

%-S

Second as a decimal number.

0, 1, …, 59

%f

Microsecond as a decimal number, zero added on the left.

000000 - 999999

%z

UTC offset in the form +HHMM or -HHMM.

%Z

Time zone name.

%j

Day of the year as a zero added decimal number.

001, 002, …, 366

%-j

Day of the year as a decimal number.

1, 2, …, 366

%U

Week number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0.

00, 01, …, 53

%W

Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0.

00, 01, …, 53

例:

# Python program to demonstrate

# strftime() function

from datetime import datetime as dt

# Getting current date and time

now = dt.now()

print("Without formatting", now)

# Example 1

s = now.strftime("%a %m %y")

print('\nExample 1:', s)

# Example 2

s = now.strftime("%A %-m %Y")

print('\nExample 2:', s)

# Example 3

s = now.strftime("%-I %p %S")

print('\nExample 3:', s)

# Example 4

s = now.strftime("%-j")

print('\nExample 4:', s)

输出:

Without formatting 2019-12-17 18:21:39.211378

Example 1:Tue-12-19

Example 2:Tuesday-12-2019

Example 3:6 PM 39

Example 4:351

python中strftime函数_Python strftime()用法及代码示例相关推荐

  1. python fmod函数_Python fmod()用法及代码示例

    fmod()函数是Python中的标准数学库函数之一,用于计算指定给定参数的模块. 用法: math.fmod( x, y ) 参数: x任何有效数字(正数或负数). y任何有效数字(正数或负数). ...

  2. python中config命令_Python config.config方法代码示例

    本文整理汇总了Python中config.config方法的典型用法代码示例.如果您正苦于以下问题:Python config.config方法的具体用法?Python config.config怎么 ...

  3. python tkinter insert函数_Python tkinter.INSERT属性代码示例

    本文整理汇总了Python中tkinter.INSERT属性的典型用法代码示例.如果您正苦于以下问题:Python tkinter.INSERT属性的具体用法?Python tkinter.INSER ...

  4. python中延时函数_Python延时操作实现方法示例

    本文实例讲述了Python延时操作实现方法.分享给大家供大家参考,具体如下: 在日常的开发中,往往会遇到这样的需求,需要某一个函数在一段时间之后才执行以达到某种特定的效果.此时,我们就需要某种机制,使 ...

  5. oracle中ln函数,PLSQL LN用法及代码示例

    LN函数是PLSQL中的内置函数,用于返回给定输入数字的自然对数.数字的自然对数是该数字与底数e的对数,其中e是近似等于2.718的数学常数.这使用lnx表示法,有时也用logex表示. 用法: LN ...

  6. python中repeat_Python Pandas Series.repeat()用法及代码示例

    Pandas 系列是带有轴标签的一维ndarray.标签不必是唯一的,但必须是可哈希的类型.该对象同时支持基于整数和基于标签的索引,并提供了许多方法来执行涉及索引的操作. Pandas Series. ...

  7. python中quad_python scipy integrate.quad用法及代码示例

    计算定积分. 使用Fortran库QUADPACK中的技术将func从a集成到b(可能是无限间隔). 参数: func:{function, scipy.LowLevelCallable}集成的Pyt ...

  8. python中 mul_Python Pandas Series.mul()用法及代码示例

    Python是进行数据分析的一种出色语言,主要是因为以数据为中心的Python软件包具有奇妙的生态系统. Pandas是其中的一种,使导入和分析数据更加容易. Python Series.mul()用 ...

  9. python中numpy.mean_Python numpy.mean()用法及代码示例

    numpy.mean(arr,axis = None):计算沿指定轴的给定数据(数组元素)的算术平均值(平均值). 参数: arr :[数组]输入数组. axis :我们要沿其计算算术平均值的[int ...

  10. python中output使用_Python output.Output方法代码示例

    # 需要导入模块: import output [as 别名] # 或者: from output import Output [as 别名] def __init__(self,param,grid ...

最新文章

  1. 提高IIS网站服务器的效率的八种方法 (转载)
  2. go实现重新给metric打标签上传到prometheus_案例分析|云原生监控Prometheus对样本rate计算,出现标签重复?...
  3. HBase-Shell-数据结构-原理
  4. 利用 Java dump 进行 JVM 故障诊断
  5. [转载]JAVA实现鼠标右键功能
  6. linux android gradle构建机器 error while loading shared libraries: libz.so.1: cannot open shared object
  7. linux get current thread count and system threads limit
  8. 牛比的表格处理模块tablib
  9. 使用submit异步提交,阻止表单默认提交
  10. java.sql.SQLException: Field 'id' doesn't have a default value解决方法
  11. xp系统蓝屏代码7b_电脑蓝屏的症状和解决办法
  12. nginx源码安装教程
  13. matlab符号函数与对其的常用操作
  14. python在linux系统下的编辑编译运行
  15. 人工智能教程1---科普人工智能
  16. php发卡v6_GitHub - Cghang/vfkphp: V发卡 完全开源免费的个人自动发卡解决方案
  17. 毕业设计成品价格_一套毕业设计多少钱
  18. 大数据开发最火技术Kafka背后的“黑科技”
  19. 数据网格(Data Mesh)是什么?
  20. 分布式中间件──断路器

热门文章

  1. 正向代理和反向代理的区别Nginx配置虚拟主机流程(后续更新)
  2. c语言结构体定义坐标,C语言结构体定义的方法汇总
  3. 分析称诺基亚WP8应学习苹果向开发者兜售梦想
  4. Apache HTTP Server 2.4.49 路径穿越漏洞(CVE-2021-41773)
  5. 反渗透设备:反渗透设备应用的基本原理与技术优势
  6. 当魔法师挥舞起大刀!
  7. 微信小程序支付,查询和支付回调
  8. 基于Java的小学数学辅助教学软件
  9. 与Perl兼容的正则表达式函数
  10. ubuntu下软件安装卸载与查看