简体   繁体   English

将yyyymmdd转换为序列号python

[英]convert yyyymmdd to serial number python

How do I convert a list of dates that are in the form yyyymmdd to a serial number? 如何将yyyymmdd格式的日期列表转换为序列号? For example, if I have this list of dates: 例如,如果我有以下日期列表:

t = [1898-10-12 06:00,1898-10-12 12:00,1932-09-30 08:00,1932-09-30 00:00]

How do I convert each date to a serial number? 如何将每个日期转换为序列号? Im currently using the datetime toordinal() command, but each date is being rounded to the same serial number. 我目前正在使用datetime toordinal()命令,但是每个日期都被四舍五入为相同的序列号。 How do I get the same dates with different times to be different numbers? 如何将不同时间的相同日期设为不同的数字?

The times in the list are the datetime.datetime numbers. 列表中的时间是datetime.datetime数字。 I tried then doing: 我尝试然后做:

thurser = []
for i in range(len(t)):
    thurser.append(t[i].toordinal())

But am not getting serial numbers as floats. 但是我没有得到浮点数的序列号。

Let me know if my understanding is wrong, I tried following and gives distinct numbers for each value of the list. 让我知道如果我的理解是错误的,我尝试进行以下操作,并为列表的每个值提供不同的数字。
I modified 我修改了
t = ['1898-10-12 06:00','1898-10-12 12:00','1932-09-30 08:00','1932-09-30 00:00']
with
t = [datetime.datetime(1898, 10, 12, 6, 0), datetime.datetime(1898, 10, 12, 12, 0), datetime.datetime(1932, 9, 30, 8, 0), datetime.datetime(1932, 9, 30, 0, 0)]
As mentioned in comment it is list of datetime.datetime. 如评论中所述,它是datetime.datetime的列表。
I am considering total MilliSeconds from 1970-01-01 00:00:00 the given date to generate a number. 我正在考虑从1970-01-01 00:00:00在给定日期生成的1970-01-01 00:00:00总数。
So dates which are before above date give values in negative. 因此,高于该日期的日期将给出负值。 But distinct values. 但价值观不同。

t = [datetime.datetime(1898, 10, 12, 6, 0), datetime.datetime(1898, 10, 12, 12, 0), datetime.datetime(1932, 9, 30, 8, 0), datetime.datetime(1932, 9, 30, 0, 0)]
thurser = []
x = []
for i in range(len(t)):
    thurser.append(t[i].toordinal())
    x.append((t[i]-datetime.datetime.utcfromtimestamp(0)).total_seconds() * 1000.0)

print(thurser)
print(x)

output: 输出:

 [693150, 693150, 705556, 705556] [-2247501600000.0, -2247480000000.0, -1175616000000.0, -1175644800000.0] 

datetime.toordinal() considers only the 'date' part of the datetime object, not the time. datetime.toordinal()仅考虑datetime对象的“日期”部分,而不考虑时间。 So does date.toordinal() - it only has a date part. date.toordinal() -它只有日期部分。 The first 2 and last 2 elements in your list have datetimes on the same date but at different times, which .toordinal ignores. 列表中的前2个元素和后2个元素的日期时间在同一日期,但在不同的时间, .toordinal忽略它们。 So, .toordinal will give you the same value for those same-dated datetimes. 因此, .toordinal将为那些相同的日期时间提供相同的值。

In general, the solution would be to calculate the delta between your dates and a pre-determined/fixed one. 通常,解决方案是计算日期与预定日期/固定日期之间的差额。 I'm using datetime.datetime(1, 1, 1) , the earliest possible datetime, so all the deltas are positive: 我正在使用datetime.datetime(1, 1, 1) ,这是最早的日期时间,因此所有增量都是正数:

thurser = []
# assuming t is a list of datetime objects
for d in t:
    delta = d - datetime.datetime(1, 1, 1)
    thurser.append(delta.days + delta.seconds/(24 * 3600))

>>> print(thurser)
[693149.25, 693149.5, 705555.3333333334, 705555.0]

And if you prefer ints instead of floats, then use seconds instead of days: 如果您更喜欢ints而不是float,那么请使用秒而不是day:

thurser.append(int(delta.total_seconds()))  # total_seconds has microseconds in the float

>>> print(thurser)
[59888095200, 59888116800, 60959980800, 60959952000]

And to get back the original values in the 2nd example: 并在第二个示例中获取原始值:

>>> [datetime.timedelta(seconds=d) + datetime.datetime(1, 1, 1) for d in thurser]
[datetime.datetime(1898, 10, 12, 6, 0), datetime.datetime(1898, 10, 12, 12, 0),
 datetime.datetime(1932, 9, 30, 8, 0), datetime.datetime(1932, 9, 30, 0, 0)]
>>> _ == t  # compare with original values
True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM