1

im working on small project and i need to display date from api , api uses millisecounds and i cant really find a way to get date without time. So far i didnt find anything usefull on internet. Code i was using for this is:

ts= millisecounds im using
date = datetime.datetime.fromtimestamp(ts / 1000, tz=datetime.timezone.utc) 
print(date)

But it prints something like 2010-10-10 10:10:10.100000+00:00 only thing i want from this is first part (2010-10-10) how can i get date?

Pewi
  • 11
  • 1
  • 5
  • Does this answer your question? [converting epoch time with milliseconds to datetime](https://stackoverflow.com/questions/21787496/converting-epoch-time-with-milliseconds-to-datetime) – GTS Apr 03 '21 at 20:49

1 Answers1

0

1. Naive Solution:

If you just want the date, you can try using the split method:

Code:

year_month_day = date.split(" ")[0]
print(year_month_day)

Output:

2010-10-10

2. Using strftime():

# using strftime                                 
ts = 1588234567899                                # Unix time in milliseconds
ts /= 1000                                        # Convert millisecondsto seconds
datetime_object = datetime.utcfromtimestamp(ts)   # Create datetime object
date = datetime_object.strftime('%Y-%m-%d')       # Strip just the date part out
print(date)

Output:

2020-04-30
GTS
  • 56
  • 6