How to print a date in a regular format?

后端 未结 22 2798
孤街浪徒
孤街浪徒 2020-11-21 23:00

This is my code:

import datetime
today = datetime.date.today()
print(today)

This prints: 2008-11-22 which is exactly what I wa

22条回答
  •  逝去的感伤
    2020-11-21 23:50

    I don't fully understand but, can use pandas for getting times in right format:

    >>> import pandas as pd
    >>> pd.to_datetime('now')
    Timestamp('2018-10-07 06:03:30')
    >>> print(pd.to_datetime('now'))
    2018-10-07 06:03:47
    >>> pd.to_datetime('now').date()
    datetime.date(2018, 10, 7)
    >>> print(pd.to_datetime('now').date())
    2018-10-07
    >>> 
    

    And:

    >>> l=[]
    >>> l.append(pd.to_datetime('now').date())
    >>> l
    [datetime.date(2018, 10, 7)]
    >>> map(str,l)
    
    >>> list(map(str,l))
    ['2018-10-07']
    

    But it's storing strings but easy to convert:

    >>> l=list(map(str,l))
    >>> list(map(pd.to_datetime,l))
    [Timestamp('2018-10-07 00:00:00')]
    

提交回复
热议问题