日期时间以奇怪的格式打印
datetime printed in strange format
当我尝试打印时出现这种奇怪的日期时间格式
(datetime.datetime(2021, 7, 12, 15, 3),)
news = News(dateTime = datetime.strptime(new["date"], EU_SHORT_FORMAT))
print(news.DateTime)
但是如果从数据库中查询对象并尝试打印它会打印出实际的日期时间
我正在使用 sqlalchemy 来管理数据库
尝试打印日期时间对象将打印对象本身,而不是日期。该对象存储日期和时间信息,允许您通过不同的方法检索它。以下是您可以使用日期时间对象执行的操作的一些示例:
>>> d = datetime.datetime.now()
>>> d # this is what you see when you print the object
datetime.datetime(2021, 7, 14, 9, 51, 56, 483458)
>>> d.ctime() # an example built in method that formats the date and time
'Wed Jul 14 09:51:56 2021'
>>> d.isoformat(" ") # this is equivalent to str(d)
'2021-07-14 09:51:56.483458'
>>> d.year, d.month, d.day, d.hour, d.minute, d.second # storing the datetime allows you to get granular values
(2021, 7, 14, 9, 51, 56)
>>> d.timestamp() # this is the number of seconds since 1970
1626281516.483458
>>> d.weekday() # 2 means Tuesday
2
>>> d.strftime("%A %d, in the month of %B, and the year of %Y") # strftime() allows you to format the date and time however you like!
'Wednesday 14, in the month of July, and the year of 2021'
>>> d.strftime("%d/%m/%y") # in many different styles
'14/07/21'
>>> d.strftime("%m/%d/%y")
'07/14/21'
当您查询数据库时,它会使用这些内置函数之一为您提供日期和时间的可读格式。
当我尝试打印时出现这种奇怪的日期时间格式
(datetime.datetime(2021, 7, 12, 15, 3),)
news = News(dateTime = datetime.strptime(new["date"], EU_SHORT_FORMAT))
print(news.DateTime)
但是如果从数据库中查询对象并尝试打印它会打印出实际的日期时间
我正在使用 sqlalchemy 来管理数据库
尝试打印日期时间对象将打印对象本身,而不是日期。该对象存储日期和时间信息,允许您通过不同的方法检索它。以下是您可以使用日期时间对象执行的操作的一些示例:
>>> d = datetime.datetime.now()
>>> d # this is what you see when you print the object
datetime.datetime(2021, 7, 14, 9, 51, 56, 483458)
>>> d.ctime() # an example built in method that formats the date and time
'Wed Jul 14 09:51:56 2021'
>>> d.isoformat(" ") # this is equivalent to str(d)
'2021-07-14 09:51:56.483458'
>>> d.year, d.month, d.day, d.hour, d.minute, d.second # storing the datetime allows you to get granular values
(2021, 7, 14, 9, 51, 56)
>>> d.timestamp() # this is the number of seconds since 1970
1626281516.483458
>>> d.weekday() # 2 means Tuesday
2
>>> d.strftime("%A %d, in the month of %B, and the year of %Y") # strftime() allows you to format the date and time however you like!
'Wednesday 14, in the month of July, and the year of 2021'
>>> d.strftime("%d/%m/%y") # in many different styles
'14/07/21'
>>> d.strftime("%m/%d/%y")
'07/14/21'
当您查询数据库时,它会使用这些内置函数之一为您提供日期和时间的可读格式。