智能日期解读

发布于 2024-07-29 01:39:28 字数 1542 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼前雾蒙蒙 2024-08-05 01:39:28

也许您正在考虑 PHP 的 strtotime() 函数,即 日期解析的瑞士军刀

伙计,我在 strtotime() 之前做了什么。 哦,我知道,我有一个 482 行函数来解析日期格式并返回时间戳。 我仍然无法做出真正酷的事情。 就像今晚我需要弄清楚美国的感恩节是什么时候。 我知道这是十一月的第四个星期四。 因此,我从一些数学知识开始,并检查 11 月 1 日是星期几。 这一切都让我头疼。 所以,我只是为了好玩而尝试这个。

strtotime("十一月,星期四".date("Y")。" + 3 周") 
  

这给了我感恩节。 太棒了。

遗憾的是,似乎没有 Python 的等效版本。 我能找到的最接近的是 dateutil.parser 模块。

Perhaps you are thinking of PHP's strtotime() function, the Swiss Army Knife of date parsing:

Man, what did I do before strtotime(). Oh, I know, I had a 482 line function to parse date formats and return timestamps. And I still could not do really cool stuff. Like tonight I needed to figure out when Thanksgiving was in the US. I knew it was the 4th Thursday in November. So, I started with some math stuff and checking what day of the week Nov. 1 would fall on. All that was making my head hurt. So, I just tried this for fun.

strtotime("thursday, november ".date("Y")." + 3 weeks")

That gives me Thanksgiving. Awesome.

Sadly, there does not appear to be a Python equivalent. The closest thing I could find is the dateutil.parser module.

猫烠⑼条掵仅有一顆心 2024-08-05 01:39:28

请参阅我的 SO回答三个Python日期解析库的链接。 其中最常用的一种是 python-dateutils 但不幸的是它无法处理所有格式。

See my SO answer for links to three Python date parsing libraries. The one of these most commonly used is python-dateutils but unfortunately it can't handle all formats.

夜巴黎 2024-08-05 01:39:28

我见过的 python 的简单例子是这里

它使用 pyparsing,只是一个小脚本,但它可以满足您的要求。 然而,优点是如果需要的话,它很容易扩展和改进。 随着解析的进行,Pyparsing 相对容易学习。

The simple one I have seen for python is here.

It uses pyparsing and is only a small script, but it does what you ask. However, the advantage being it would be pretty easy to extend and improve on if needed. Pyparsing is relatively easy to learn as parsing goes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文