如何将多个 python 日期时间实例格式化为简单格式?

发布于 01-06 23:53 字数 332 浏览 2 评论 0原文

我有一个 Django 应用程序,需要获取多个日期时间的列表并打印出一个简单的字符串来解释模式是什么。

示例:

对于星期一、星期三和星期五下午 3 点的 3 个日期时间实例,简单的输出将类似于 星期一、星期三、星期五下午 3:00

对于每月 4 号的 3 个日期时间实例,输出将是每个月的 4 号

我进行了搜索,但没有找到 Python 或 Django 中内置的任何内容,但是有人知道有任何可以执行类似操作的开源脚本吗?我知道这样做的唯一方法是手动尝试匹配日期时间列表的模式,这听起来像是很多 if ... else ... 语句。

I've got a Django app that needs to take a list of multiple datetimes and print out a simple string that explains what the pattern is.

Example:

With 3 datetime instances for Monday, Wednesday, and Friday at 3pm, the simple output would be something like Monday, Wednesday, Friday at 3:00pm

With 3 datetime instances of the 4th of every month, the output would be The 4th of every month

I searched and didn't find anything built into Python or Django, but does anyone know of any open source scripts that can do something similar? The only way I would know of doing it is manually trying to match the patterns of the list of datetimes and that sounds like a lot of if ... else ... statements.

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

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

发布评论

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

评论(2

你是我的挚爱i2025-01-13 23:53:57

这听起来像是根本上的“业务逻辑”。例如,如果这些日期连续几年都恰好是复活节,您是否希望该日期被采纳?如果都是光明节的最后一天怎么办?这不太可能是由语言或库直接处理的事情——我想你需要自己编写逻辑和规则。这里肯定有创新的空间 - 考虑如何以统一的方式表达规则......查看现有的时区数据库和其他此类现有软件以获取灵感(或警告要避免什么!)。

It sounds like something that is fundamentally "business logic." For example, if the dates happen to all be Easter on consecutive years, do you expect that to be picked up? What about if they are all the last day of Hanukkah? This isn't likely to be something that's handled by a language or library directly--you'll need to write the logic and rules yourself, I imagine. There's definitely room to be innovative here--think about how you can express the rules in a uniform way...look at the existing timezone database and other such existing software for inspiration (or warnings of what to avoid!).

晚雾2025-01-13 23:53:57

根据您的需要,您必须使用 Python Calendar 模块来迭代工作日、几天或一个月。它提供了在每月或每周的特定日期阅读的功能。

As per your need you have to use Python Calendar module to iterate over the weekdays or days or the month. It provide the functions to read on specific day of month or week.

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