Friday, January 18, 2013

Julian date in python

I was recently looking for simple function to convert date to Julian Date in python. Julian date is number of days since November 24, 4714 BC. It's used by astronomers and by Google in datarange queries ;] I've found working, pretty and simple function here: http://code.activestate.com/recipes/117215/ Here is the code:
import datetime

def date_to_julian_day(my_date):
    """Returns the Julian day number of a date."""
    a = (14 - my_date.month)//12
    y = my_date.year + 4800 - a
    m = my_date.month + 12*a - 3
    return my_date.day + ((153*m + 2)//5) + 365*y + y//4 - y//100 + y//400 - 32045

1 comment:

  1. Hi, according to

    http://en.wikipedia.org/wiki/Julian_day, there are actually two possible different start dates.

    January 1, 4713 BC (proleptic Julian calendar)

    and

    November 24, 4714 BC, (proleptic Gregorian calendar)

    ReplyDelete