-1

I want to get date and time format in python from Julian day. I have an hdf file t1.15008.0239.mod07.hdf and I have tried to convert Julian day '15008' to datetime like this:

def julday_to_masehi(julday):
    file_date= julday
    file_date = datetime.strptime(file_date, '%y%j')
    file_date = file_date.strftime('%Y-%m-%d')
    return file_date

julday = '15232'
datetimee = julday_to_masehi(julday)
print(datetimee)

and the result is 2015-08-20.

But I also need the time. If I try julday = '150080239' and change the to code to: file_date = file_date.strftime('%Y-%m-%d %H:%M:%S'), then I get an error.

So how can I convert Julian day '150080239' into python datetime in format '%Y-%m-%d %H:%M:%S'?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • You might want to peruse the Python datetime documentation. It's currently at https://docs.python.org/2/library/datetime.html. There are a lot of good manipulation methods there. – Prune Oct 01 '15 at 00:16
  • 1
    ["julian day" may mean different things in different cases](http://stackoverflow.com/a/25831416/4279). What result do you expect for `00232` and `100232` ? – jfs Oct 02 '15 at 09:11
  • [read the description of `%H`, `%M` time formats](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior) – jfs Oct 02 '15 at 09:13

1 Answers1

0
date, time = jul_date_time.split('.')
hour = int(time[:2])
minute = int(time[2:])

You already showed you can handle the date; this lets you construct a Python time -- and datetime -- from the pieces of the input string.

Is that what you need?

Prune
  • 76,765
  • 14
  • 60
  • 81
  • no, the time '0239' is julian day. oh i;m sorry i forget to show the piece of the code. i just send part of the date like this julday = '15232' and send it to funtion like this:datetimee = julday_to_masehi(julday). Of course it just return the date like '20150820'.i think it's need to change the code of function file_date = datetime.strptime(file_date, '%y%j'), but i don't know how? – Rofiqoh Mumtahanah El Sera Oct 01 '15 at 00:47
  • Please add the missing code to your question; it's hard to read in this format. Also, please describe how you know the time of day. I thought that "15008" is supposed to be 2015, 08 Jan (8th day of the year). You'll have to describe your inputs in detail if you want a proper solution. – Prune Oct 01 '15 at 00:54