I have a string "2012.11.07" in python. I need to convert it to date object and then get an integer value of day of year and also Julian day. Is it possible?
I have a string 2012.11.07 in python. I need to convert it to date
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First, you can convert it to a
datetime.datetimeobject like this:Then you can use the methods on
datetimeto get what you want… except thatdatetimedoesn’t have the function you want directly, so you need to convert to a time tupleThe term “Julian day” has a few different meanings. If you’re looking for
2012312, you have to do that indirectly, e.g., one of the following.If you’re looking for a different meaning, you should be able to figure it out from here. For example, if you want the “days since 1 Jan 4713 BC” meaning, and you have a formula that requires Gregorian year and day in year, you’ve got those two values above to plug in. (If you have a formula that takes Gregorian year, month, and day, you don’t even need the
timetuplestep.) If you can’t work out where to go from there, ask for further details.If you don’t have a formula—and maybe even if you already do—your best bet is probably to look around PyPI and ActiveState for pre-existing modules. For example, a quick search turned up something called
jdcal. I’d never seen it before, but a quickpip install jdcaland a brief skim of the readme, and I was able to do this:That’s the same result that the USN Julian date converter gave me.
If you want integral Julian day, instead of fractional Julian date, you have to decide which direction you want to round—toward 0, toward negative infinity, rounding noon up to the next day, rounding noon toward even days, etc. (Note that Julian date is defined as starting since noon on 1 Jan 4713BC, so half of 7 Nov 2012 is 2456238, the other half is 2456239, and only you know which one of those you want…) For example, to round toward 0: