Niknak
Niknak

Reputation: 593

Convert calendar date to julian date - python

I am trying to convert calendar date like so: '2018-08-07' to julian calendar day like so '219'. I have tried for a long time and seem to run out of ideas. The julian day calendar is this one I am using: https://landweb.modaps.eosdis.nasa.gov/browse/calendar.html

This is what I have so far:

from datetime import date
import datetime
from PyAstronomy import pyasl

df = datetime.datetime(2018, 8, 7, 12)
print(df)
jul = pyasl.jdcnv(df)
print(jul)

Upvotes: 1

Views: 7600

Answers (2)

Teemo
Teemo

Reputation: 860

def date_2_julian_string (date):
    return str(date.strftime('%y')) + date.strftime('%j')

Upvotes: 0

sacuL
sacuL

Reputation: 51395

You can use strftime for this (see this for a description of directives to use):

jul = df.strftime('%j')
>>> jul
'219'

Upvotes: 1

Related Questions