Python3 获取纪元时间的第一天和最后一天
给定一个月和一年,例如03 2022
,我需要获取该月第一天和该月最后一天的纪元时间。我不知道该怎么做。任何帮助将不胜感激。谢谢。
Given a month and a year, such as 03 2022
, I need to get the epoch time of the first day of the month and the last day of the month. I am not sure how to do that. Any help would be appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注意: 请参阅下面的 @segaps 答案;我原来的答案在
(dt.month + 1) // 13
中将楼层除以 12 而不是 13,这给出了 11 月份的错误结果。1 请注意,Unix 时间始终表示带有时间的日期,而不仅仅是日期。
Note: see @segaps answer below; my original answer had a floor division by 12 instead of 13 in
(dt.month + 1) // 13
, which gives incorrect result for the month of November.1 Note that Unix time always represents a date with a time, not only a date.
由于声誉无法发表评论,但 @FObersteiner 非常好,只是我建议进行一些小改动。
例如,运行当前代码将在 2022 年 11 月生成此代码
--->
注意年份字段
我建议以下
差异是按 13 而不是 12 进行底面划分来处理 11 月份。
将偏移量更改为秒增量,因为我觉得用户(以及来寻找答案的我自己)想要开始纪元时间和结束纪元时间,所以
11 月 1 日 00:00:00 --> 11 月 30 日 23:59:59 会比
11 月 1 日 00:00:00 --> 11 月 30 日 00:00:00 (损失一天的秒数)
上述输出将是
:
Unable to comment due to reputation but @FObersteiner is excellent just I would recommend a small change.
For example running the current code it would produce this for Nov 2022
--->
Note the year field
I'd suggest the following
Differences being floor division by 13 instead of 12 to handle the month of November.
Changing the offset to seconds delta because I felt the user ( and myself who came looking for the answer) wanted the starting epoch time and the ending epoch time so
Nov 1st 00:00:00 --> Nov 30th 23:59:59 would be better than
Nov 1st 00:00:00 --> Nov 30th 00:00:00 ( Losing a day worth of seconds)
Output of the above would be
: