Time isn't valid but code works- grand finale


#1



6. Grand Finale


no error messages but this is what prints to the console: 8/27/2016 1:32:4
None


the time isn't a time. it is 9:30 PM EST btw


Replace this line with your code. 
from datetime import datetime
now = datetime.now()

print '%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year, now.hour, now.minute, now.second)

why is it doing this???? I get no error messages and it runs fine. Please help


#2

Take a look at documentation -> https://docs.python.org/2/library/datetime.html#datetime.datetime.now

datetime.now returns local time. This code is not executed by your computer, but by the codecademy server. That is why the time is not the same as on your computer.

If you want to, you can specify the timezone. For my location, Poland (UTC +2) it would be:

from datetime import datetime, timedelta
now = datetime.utcnow() + timedelta(hours = 2)

print '%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year, now.hour, now.minute, now.second)

or you can use a pytz package -> https://pypi.python.org/pypi/pytz


#3

Thank you! Appreciate it!


#4

You're very welcome :slight_smile:


#5

Just cuious: Any idea where the CodeAcademy servers are located? From what I can tell, the time difference puts it somewhere is 6 hours ahead of where I am (Seattle, WA). Are they located in ... Iceland? :slight_smile:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.