在Python中将日期时间转换为序数失败 [英] Convert datetime to ordinal in python fails
本文介绍了在Python中将日期时间转换为序数失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我曾与日期合作。我想将日期时间转换为序数,但脚本失败。
i been had worked with dates. I want to convert datetimes to ordinal but my script fails.
from datetime import datetime
date = "2016/12/07 17:45"
date_strip = datetime.strptime(date, '%Y/%m/%d %H:%M').date()
ordinal = date_strip.toordinal()
# ordinal = 736305
normal_date = datetime.fromordinal(ordinal)
# normal_date = 2016-12-07 00:00:00 not 2016-12-07 17:45:00
怎么了?他们可以帮我吗?
whats wrong? ca they help me please?
推荐答案
因为 ordinal
仅存储日期。该序数不能包含时间:
Because ordinal
stores just the date. the ordinal cannot contain the time:
该日期的公历格里高里序数,其中第一年的1月1日具有序数1。对于任何日期对象d,date.fromordinal(d.toordinal())== d。
the proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1. For any date object d, date.fromordinal(d.toordinal()) == d.
如果要转换为时间戳或从时间戳转换常见问题,请尝试如此回答。
If you want to convert to/from timestamps instead of ordinals, try this so answer.
这篇关于在Python中将日期时间转换为序数失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文