Home > other >  Python: use datetime and ephem to get date only (without time)
Python: use datetime and ephem to get date only (without time)

Time:12-22

I'm pretty new to python and I'm currently trying to write a code for a small project. My problem is that when I execute my code I get the date time and I'm only interested in the date. I've tried googling the problem but haven't found a solution for using datetime and ephem together (I have to use ep.date(datetime(year, month, day)) so I can use the input date with other dates that I get from ephem).

This is a small example code of what I'm doing:

from datetime import datetime
import ephem as ep #explanation

input_date =input("Please enter the date you you'd like to know the moon phase for in the YYYY-MM-DD format: " )
year, month, day = map(int, input_date.split('-'))
datetime(int(year), int(month), int(day))
new_date = ep.date(datetime(year, month, day))

print(new_date)

And this is my output: https://i.stack.imgur.com/0VJQM.png

If you click on the link you'll see that I get 2020/2/2 00:00:00 for the input 2020-2-2, it doesn't make my code stop working, but because I'll display this date quite often, I'd like to remove the time and only have the date.

CodePudding user response:

You can use ‘from datetime import date’ instead of ‘datetime’

CodePudding user response:

If you just need to display the date and don't need further calculations, you can convert the ephem Date type back into a Python datetime type, then convert the Python datetime into a date.

from datetime import datetime
import ephem as ep #explanation

input_date =input("Please enter the date you you'd like to know the moon phase for in the YYYY-MM-DD format: " )
year, month, day = map(int, input_date.split('-'))
ephem_date = ep.date(datetime(year, month, day))
python_date_only = new_date.datetime().date()

print(python_date_only)
  • Related