简体   繁体   中英

convert yyyymmdd to serial number python

How do I convert a list of dates that are in the form yyyymmdd to a serial number? 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. How do I get the same dates with different times to be different numbers?

The times in the list are the datetime.datetime numbers. 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.
I am considering total MilliSeconds from 1970-01-01 00:00:00 the given date to generate a number.
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. So does date.toordinal() - it only has a date part. The first 2 and last 2 elements in your list have datetimes on the same date but at different times, which .toordinal ignores. So, .toordinal will give you the same value for those same-dated datetimes.

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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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