Skip to content

Instantly share code, notes, and snippets.

@eamanu
Created August 23, 2018 13:36
Show Gist options
  • Save eamanu/369e0de2fe6616bfaa1fb6bbbef07300 to your computer and use it in GitHub Desktop.
Save eamanu/369e0de2fe6616bfaa1fb6bbbef07300 to your computer and use it in GitHub Desktop.
Calculate from datetime ot JDN
def dt_to_jdn(dt):
"""Convert datetime to Julian Day Number
.. warning: Please verify this calculating method
This convertion is based on:
'Practical Astronomy with your Calculator or Spreadsheet',
4th ed., Duffet-Smith and Zwart, 2011.
https://en.wikipedia.org/wiki/Julian_day
:param dt: datetime to convert. This must be on UTC.
:type dt: datetime.datetime
:return: Julian Date
:Examples:
.. code-block:: console
import datetime
from utils import *
dt_to_jdn(datetime.datetime.now())
2458352.5651478795
"""
year = dt.year
month = dt.month
day = dt.day
hours = dt.hour
min = dt.minute
sec = dt.second
micro = dt.microsecond
if month == 1 or month == 2:
year_ = year - 1
month_ = month + 12
else:
year_ = year
month_ = month
if dt > datetime.datetime(1582, 10, 15):
A = math.trunc(year_/100)
B = 2 - A + math.trunc(A/4)
else:
B = 0
if year_ < 0:
C = math.trunc((365.25 * year_) - 0.75)
else:
C = math.trunc(365.25 * year_)
D = math.trunc(30.6001 * (month_ + 1))
JD = B + C + D + day + 1720994.5
decimal_part = ((hours - 12)/24.) + ((min)/1440.) + ((sec)/86400.) + ((micro)/8.64e10)
JDN = JD + decimal_part
return JDN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment