我在Python中將datetime
字符串轉換爲datetime
對象時看到很多,但我想以其他方式。
我有datetime.datetime(2012, 2, 23, 0, 0)
,我想將它轉換爲字符串,如'2/23/2012'
。將日期時間對象轉換爲Python中的日期字符串
回答
你可以使用簡單的字符串格式化方法:
>>> dt = datetime.datetime(2012, 2, 23, 0, 0)
>>> '{0.month}/{0.day}/{0.year}'.format(dt)
'2/23/2012'
>>> '%s/%s/%s' % (dt.month, dt.day, dt.year)
'2/23/2012'
類似地,你可以做''{:%Y-%m-%d%H:%M}'格式(datetime(2001,2,3,4,5))''。在pyformat.info查看更多 – mway
type-specific formatting
可以用作良好:
t = datetime.datetime(2012, 2, 23, 0, 0)
"{:%m/%d/%Y}".format(t)
輸出:
'02/23/2012'
date
和datetime
對象(和time
以及)支持mini-language to specify output,有兩種方法來訪問它:
- 直接調用方法:
dt.strftime('format here')
;和 - 新格式的方法:
'{:format here}'.format(dt)
所以,你的例子可能看起來像:
dt.strftime('%m/%d/%Y')
或
'{:%m/%d/%Y}'.format(dt)
爲了完整性:您也可以直接訪問的屬性的對象,但你只能得到的數字:
'%s/%s/%s' % (dt.month, dt.day, dt.year)
學習迷你語言所花費的時間是值得的。
供參考,在這裏是在小型語言使用的代碼:
%a
平日的語言環境的縮寫名稱。%A
工作日爲區域設置的全名。%w
平日爲十進制數字,其中0表示星期日,6表示星期六。%d
當月的一天爲零填充十進制數。%b
月份爲區域設置的縮寫名稱。%B
月份作爲語言環境的全名。%m
月爲零填充十進制數。 01,...,%y
沒有世紀的一年作爲零填充十進制數。 00,...,99%Y
以十進制數表示的世紀。 1970,1988,2001,2013%H
小時(24小時制)作爲零填充十進制數。 00,...,23%I
小時(12小時制)作爲零填充十進制數。 01,...,12%p
Locale相當於AM或PM。%M
分鐘爲零填充十進制數字。 00,...,59%S
第二個作爲零填充的十進制數。 00,...,59%f
微秒作爲十進制數,左側填零。 000000,...,999999%z
UTC形式偏移+ HHMM或-HHMM(空if幼稚),+0000,-0400,1030%Z
時區名稱(空如果幼稚),UTC, EST,CST%j
一年中的一天爲零填充十進制數。 001,...,366%U
一年中的星期數(星期日是第一個)作爲零填充十進制數。%W
年的週數(星期一是第一個)作爲十進制數。%c
區域設置的適當日期和時間表示。%x
區域設置的相應日期表示。%X
區域設置的適當時間表示。%%
字面'%'字符。
賞金在這裏總結得很好 – tinySandy
字符串連接,str.join
,可用於構建字符串。
d = datetime.now()
'/'.join(str(x) for x in (d.month, d.day, d.year))
'3/7/2016'
另一種選擇:
import datetime
now=datetime.datetime.now()
now.isoformat()
# ouptut --> '2016-03-09T08:18:20.860968'
可以時間值轉換爲字符串。 published_at="{}".format(self.published_at)
通過直接處理日期時間對象的組件,可以將日期時間對象轉換爲字符串。
from datetime import date
myDate = date.today()
#print(myDate) would output 2017-05-23 because that is today
#reassign the myDate variable to myDate = myDate.month
#then you could print(myDate.month) and you would get 5 as an integer
dateStr = str(myDate.month)+ "/" + str(myDate.day) + "/" + str(myDate.year)
# myDate.month is equal to 5 as an integer, i use str() to change it to a
# string I add(+)the "/" so now I have "5/" then myDate.day is 23 as
# an integer i change it to a string with str() and it is added to the "5/"
# to get "5/23" and then I add another "/" now we have "5/23/" next is the
# year which is 2017 as an integer, I use the function str() to change it to
# a string and add it to the rest of the string. Now we have "5/23/2017" as
# a string. The final line prints the string.
print(dateStr)
輸出 - > 2017年5月23日
請使用[編輯]鏈接來解釋此代碼的工作原理,而不只是給出代碼,因爲解釋更有可能幫助未來的讀者。另見[回答]。 [源(http://stackoverflow.com/users/5244995) –
- 1. 將日期字符串轉換爲日期時間對象
- 2. 從日期時間字符串轉換爲JavaScript日期對象
- 3. 將日期字符串轉換爲Javascript中的日期對象
- 4. 將字符串日期時間轉換爲Ruby日期時間
- 5. 將日期時間字符串轉換爲日期時間
- 6. 將日期字符串轉換爲SSIS中的日期時間
- 7. 將Android的日期字符串轉換爲日期對象
- 8. 將2種類型的日期字符串轉換爲日期時間對象
- 9. Js將日期對象轉換爲python日期時間
- 10. 將日期時間轉換爲代表日期的字符串
- 11. 將字符串日期轉換爲日期對象
- 12. 將字符串日期轉換爲日期對象
- 13. 將GMT日期字符串轉換爲本地日期對象
- 14. 將日期字符串轉換爲日期對象Javascript
- 15. 如何將字符串日期轉換爲日期類對象
- 16. 將日期字符串轉換爲日期對象
- 17. 將此字符串轉換爲日期時間對象
- 18. 將字符串轉換爲日期時間對象
- 19. 將UTC日期轉換爲日期時間字符串Javascript
- 20. 將日期字符串轉換爲日期時間
- 21. javascript日期/時間字符串轉換爲SQL日期時間
- 22. Perl - 將日期時間戳轉換爲日期時間對象
- 23. 在Python中將可讀的日期和時間字符串轉換爲日期時間對象
- 24. 轉換Python字符串日期MySQL的日期時間
- 25. Python的轉換日期字符串到日期時間
- 26. 將字符串轉換爲日期python
- 27. 如何在python中將字符串日期轉換爲日期時間格式?
- 28. Python將字符串日期對象轉換爲日期給出的值errorerror
- 29. 將對象轉換爲日期時間
- 30. 將日期字符串轉換爲日期對象在Firefox中時出錯
非常用於'DateTimeField'或'DateField'在Django有用。謝謝 – erajuan
使用't = datetime.datetime.now()'使用當前日期 – gizzmole
據我可以從文檔中看出,無法返回非零填充日期,即'2/23/2012'。 –