python-3.x - Python字符串到日期时间日期
问题描述
我有很多这样的日期:(16.8.18
美国:2018 年 8 月 16 日)字符串类型。现在,我需要检查日期是过去还是将来,但datetime
不支持德语格式。
我怎样才能做到这一点?
解决方案
from datetime import datetime
s = "16.8.18"
d = datetime.strptime(s, "%d.%m.%y")
if d > datetime.now():
print('Date is in the future.')
else:
print('Date is in the past.')
打印(今天是 20.7.2018):
Date is in the future.
使用的格式在手册页strptime()
中有解释。
推荐阅读
- macos - 为什么当我键入映射到其他内容的键时 IntelliJ 会插入 unicode 字符?
- javascript - 将 JSON 数据 blob 文件写入磁盘。什么是正确的内容类型?
- ios - 从 viewDidLoad 中调用的函数外部重新加载 UITableViewController
- php - PHP文件没有找到类,但是定义了类
- swift - count, utf8.count, utf16.count 返回不同的结果
- c# - C# 使用正则表达式捕获第一个匹配项
- python - 无法读取远程 Ubuntu 服务器上烧瓶中保存的机器学习模型
- javascript - 如何在 openSUSE 上使用 NGINX 或 Apache2 将我的端口 80 重新路由到 localhost:3000 以便我可以运行我的 Node.js 应用程序
- android - Android无限动画
- javascript - 如何在 TypeScript 中编写 ES6 箭头函数?