Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Dates and Times in Python (2014) Dates and Times now() and .replace()

date and times

Create a new variable called two that holds the value of now with the hour set to 14. Use the .replace() method

dt.py
import datetime
dir(datetime)
now=datetime.datetime.now()

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

OK, you have now set to .now(), but you need to do the rest of prompt and change the hour to 14.

I'm totally stuck on the next step. Here's what I've got:

import datetime

now = datetime.datetime.now()

two = now.hour.replace(14)

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You want now.replace(hour=14) instead of what you have.

Thanks, Kenneth, that totally worked. Is there a simple reason that we can access those things via dot notation but in this instance needed to use replace in that manner?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

They're read-only. You have to use .replace() to write to them.

Since you have declared now as a variable that holds the outcome of datetime.datetime.now(), whenever you need to use datetime.datetime.now(), use now() instead. The .replace() method is being used to change/set the value of hour to a new entity, that is, 14. Therefore, .replace() is functioning within the now() variable.

This is what you should end up with:

import datetime
now = (datetime.datetime.now())
two = now.replace(hour=14)
Jose Luis Lopez
Jose Luis Lopez
19,179 Points

import datetime

now = datetime.datetime.now()

two = now.replace(hour=14)