开始使用免费开始使用

使用 datetime 库

我们将探索 datetime 库,重点是 datetime.strptime(date_string, format) 方法。它通过识别日、月、年,将特定格式的日期字符串转换为日期对象。请根据下表,从下面的日期中识别年份、月份和日期,并写出合适的代码(您可以在控制台打印该方法的返回值来检查答案):

  • 12-01-1990(美国格式 m-d-y)
  • 23/08/2020(欧洲格式 d-m-y)
Directive Meaning Example
%d Day of the month as a zero-padded decimal number 01, 02, …, 31
%b Month as locale's abbreviated name Jan, Feb, …, Dec
%B Month as locale's full name January, …, December
%m Month as a zero-padded decimal number 01, 02, …, 12
%y Year without century as a zero-padded decimal number 00, 01, …, 99
%Y Year with century as a decimal number 1970, 1988, 2001, 2013

本练习是课程的一部分

Python 金融预测

查看课程

动手互动练习

通过我们的互动练习之一,将理论转化为实践

开始练习