내가 보고 있는 문자열 변환 충족되었으며 '에서 datetime' 객체에는 많이유 데이트였어 파이썬, 하지만 난 로렌아줌마가 다른 길로 가고.
datetime.datetime(2012, 2, 23, 0, 0)
& # 39 와 같은 구체화하십시오 迈向 변환할지 싶다 ',' # 39, 2/23/2012&.
[스트라프티메] [1] 데이트였어 포맷해야 돕기 위해 사용할 수 있습니다.
E. g.,
import datetime
t = datetime.datetime(2012, 2, 23, 0, 0)
t.strftime('%m/%d/%Y')
는 용량:
'02/23/2012'
서식 지켜보리니 대한 자세한 내용은 [here] [2]
[1]: http://docs.python.org/2/library/time.html # 타임벌린스트라프티메 [2]: http://docs.python.org/library/datetime.html # 스트라프티메 strptime 비헤이비어를
날짜 (및 '시간' 에서도 '와' datetime '객체' 를 지원할 [미니 언어 지정할 수 출력입니다] (https://docs.python.org/3/library/datetime.html # 스트라프티메 및 strptime 비헤이비어를), 그리고 두 가지 방법으로 액세스하도록:
따라서 다음과 같은 예를 볼 수 있었다
dt.strftime('%m/%d/%Y')
또는
'{:%m/%d/%Y}'.format(dt)
'%s/%s/%s' % (dt.month, dt.day, dt.year)
배우는 데 걸리는 시간이 미니 언어 그만한 가치가 있다.
참고로, 다음은 미니 언어 사용되는 코드:
문자열로 변환하는 것이 datetime 객체에는 작업을 통해 직접 협력합니다 요소이라고 datetime 객체에는.
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)
출력입니다 > -; 5/23/2017
Datetime string 으로 변환할 수 있습니다.
published_at = "{}".format(self.published_at)
',' 문자열 연결 문자열을 구축하십시오 스트리요린 데 사용할 수 있습니다.
d = datetime.now()
'/'.join(str(x) for x in (d.month, d.day, d.year))
'3/7/2016'
간단한 방법을 찾는 경우 '문자열 변환 형식을 생략하십시오' datetime 수 있습니다. '다음' 와 'datetime' 객체를 이용해 어레이입니다 str 변환할 수 있습니다.
In [1]: from datetime import datetime
In [2]: now = datetime.now()
In [3]: str(now)
Out[3]: '2019-04-26 18:03:50.941332'
In [5]: str(now)[:10]
Out[5]: '2019-04-26'
In [6]: str(now)[:19]
Out[6]: '2019-04-26 18:03:50'
그러나 다음과 같은 일이다. 다른 솔루션뀉뀉뀉뀉 오를 경우 한 때 '없음' 이 경우 '' 아트리부텔라로 가변입니다 # 39, & # 39 None& 받게 될 것이다. ',' 구체화하십시오.
In [9]: str(None)[:19]
Out[9]: 'None'