Python `YYYY-MM-DD`

发布于 2024-09-14 05:37:51 字数 124 浏览 3 评论 0原文

在Python中,获得的最好方法是什么 RFC 3339 YYYY-MM-DD 来自 gtk.Calendar::get_date() 输出的文本?

In Python, what is the best way to get the
RFC 3339 YYYY-MM-DD text from the output of gtk.Calendar::get_date()?

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

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

发布评论

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

评论(3

可是我不能没有你 2024-09-21 05:37:52

感谢 Mark 和 treeface 提供的解决方案,但似乎我发明了一个更好的解决方案:

year, month, day = cal.get_date()
return '{0:04d}-{1:02d}-{2:02d}'.format(year, month+1, day)

这个解决方案更短,更容易理解(在我看来),并且我相信需要更少的处理,因为它消除了元组所在的阶段转换成 UNIX 时间。

Thanks to Mark and treeface for the solutions, but it seems I've invented a better one:

year, month, day = cal.get_date()
return '{0:04d}-{1:02d}-{2:02d}'.format(year, month+1, day)

This solution is shorter, easier to understand (in my opinion), and I believe takes less processing as it cuts out the stage where the tuple is converted into UNIX time.

删除会话 2024-09-21 05:37:52

根据 文档, get_date 返回一个 (year,month,day) 元组,其中月份为 0 到 11,日期为 1 到 31,因此:

import datetime
dtTuple = calControl.get_date()
dtObj = datetime.datetime(dtTuple[0], dtTuple[1] + 1, dtTuple[2]) #add one to month to make it 1 to 12
print dtObj.strftime("%Y-%m-%d")

According to the docs, the get_date returns a tuple of (year,month,day), where month 0 to 11 and day is 1 to 31, so:

import datetime
dtTuple = calControl.get_date()
dtObj = datetime.datetime(dtTuple[0], dtTuple[1] + 1, dtTuple[2]) #add one to month to make it 1 to 12
print dtObj.strftime("%Y-%m-%d")
北音执念 2024-09-21 05:37:52

这可能对您有用:

http://www.tutorialspoint.com/python/time_strftime.htm

我也刚刚四处寻找这个 gtk get_date 方法,我能够找到一些处理它的代码,如下所示:

//here replace "self.window.get_date()" with "gtk.Calendar::get_date()"
year, month, day = self.window.get_date()
mytime = time.mktime((year, month+1, day, 0, 0, 0, 0, 0, 0))
return time.strftime("%x", time.gmtime(mytime))

所以只需将“%x”替换为“%G-%m-%d”,然后我认为这会起作用。或者至少值得一试。

This might be useful to you:

http://www.tutorialspoint.com/python/time_strftime.htm

I've also just looked around for this gtk get_date method and I was able to find some code that handles it like this:

//here replace "self.window.get_date()" with "gtk.Calendar::get_date()"
year, month, day = self.window.get_date()
mytime = time.mktime((year, month+1, day, 0, 0, 0, 0, 0, 0))
return time.strftime("%x", time.gmtime(mytime))

So just replace "%x" with "%G-%m-%d" and I think it would work. Or at least it's worth a try.

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