簡體   English   中英

Python`YYYY-MM-DD`

[英]Python `YYYY-MM-DD`

在Python中,從gtk.Calendar::get_date()的輸出中獲取RFC 3339 YYYY-MM-DD文本的最佳方法是什么?

感謝Mark和treeface提供的解決方案,但看來我發明了一個更好的解決方案:

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

該解決方案更短,更容易理解(我認為),而且我相信它將減少將元組轉換為UNIX時間的階段,從而減少了處理。

根據docs ,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")

這可能對您有用:

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”,我認為它會起作用。 或者至少值得一試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM