2
\$\begingroup\$

I have watched a few youtube videos, and I am trying to learn a little bit more by doing. I had a blast trying to figure this out. I know it has issues such as not being able to go below 2001 and using an average for months. I'm not seeking to improve its capabilities, although i would certainly appreciate any ideas, seeking a review of the code and ways to improve. Thank you!

The code works

sd = input("Starting Julien Date (Enter as '00-000')")
ed = input("Ending Julian Date (Enter as '00-000')")

# This section gives you days from sd to ed
cy = int(ed[:2])
cd = int(ed[3:])

subyear = int(sd[:2])
yeardaycount = (cy - subyear) * 365

subdays = int(sd[3:])
daycount = cd - subdays

finalcount = yeardaycount + daycount

# This breaks the Finalcount into day, month, year.
y = finalcount / 365
y = int(y)
m = (finalcount - (y * 365)) / 30.4166666667
m = int(m)
d = finalcount - ((y * 365) + (m * 30.4166666667))
d = int(d)

print("From %s to %s, ~on average~, it has beeen %s days, %s months, and %s years." % (sd, ed, d, m, y))
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Apply datetime — Basic date and time types and third-party package dateutil as follows (given a script with hard-coded initial values):

sd = '03-023' # input("Starting Julien Date (Enter as 'yy-ddd')")
ed = '22-088' # input("Ending Julian Date (Enter as 'yy-ddd')")

# This section gives you days from sd to ed
cy = int(ed[:2])
cd = int(ed[3:])
subyear = int(sd[:2])
yeardaycount = (cy - subyear) * 365
subdays = int(sd[3:])
daycount = cd - subdays
finalcount = yeardaycount + daycount
# This breaks the Finalcount into day, month, year.
y = finalcount / 365
y = int(y)
m = (finalcount - (y * 365)) / 30.4166666667
m = int(m)
d = finalcount - ((y * 365) + (m * 30.4166666667))
d = int(d)

print("From %s to %s, ~on average~, it has beeen %s days, %s months, and %s years." % (sd, ed, d, m, y))

### using libraries:

from datetime import datetime
from dateutil import relativedelta

date_a = datetime.strptime( sd, "%y-%j")
date_b = datetime.strptime( ed, "%y-%j")

diff = relativedelta.relativedelta( date_b, date_a)

ms = diff.months
ys = diff.years
ds = diff.days

print("From %s to %s, ~   exactly~, it has beeen %s days, %s months, and %s years." % (sd, ed, ds, ms, ys))

Output: .\CR\273990.py

From 03-023 to 22-088, ~on average~, it has beeen 4 days, 2 months, and 19 years.
From 03-023 to 22-088, ~   exactly~, it has beeen 6 days, 2 months, and 19 years.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.