S.Kociok
S.Kociok

Reputation: 169

Get date and time out of julian string

i have a list of julian date: 42304.90625

I startet like this:

jDate=42304.90625  
jDate=jDate[0:5] 
print(datetime.datetime.strptime(jDate, '%y%j').date()

How can i extract the time? Can somebody help me?

Thanks

Upvotes: 0

Views: 215

Answers (1)

BoarGules
BoarGules

Reputation: 16941

From your use of %j in the conversion I take it you mean a mainframe "Julian" date of the form YYDDD. Start by splitting the value into an integer part and a fractional part:

>>> date, time = divmod(jDate, 1.0)
>>> date
42304.0
>>> time
0.90625

Using divmod() like this doesn't work on negative numbers, but a Julian date is never negative. Now convert the date to a string. A correct way to do that is:

date = str(int(date))

Doing this: jDate=jDate[0:5] doesn't work because jDate is a float. (The title of your posting says it's a string but your code says it's a float.)

Next convert the fractional day into seconds:

>>> datetime.timedelta(days=time)
datetime.timedelta(0, 78300)

and add the two together:

>>> datetime.datetime.strptime(date, '%y%j') + datetime.timedelta(days=0.90625)
datetime.datetime(2042, 10, 31, 21, 45)

Upvotes: 1

Related Questions