Datetime.now()

When I set datetime.now() to a variable (see example below), the value keeps updating each time the variable is accessed (instead of just setting the value as the time that the variable was initially declared). How do I capture the current time and store it as a variable value without it changing?

timestamp = datetime.now() 

print(timestamp) 

Can you post your code, please? Make sure to format it correctly.

1 Like

I have updated the original post to include a code example.

1 Like

Can you post your output as well? This is what I get when I run that code:


The outputs are all the same.

1 Like

Okay, whoops, I confused myself at first:

It isn’t that printing that variable more than once in a row results in different values, it’s that it shows a different value each time I run the code (which would obviously produce a different result).

Sorry about that. My critical thinking had a wrench in it for a moment.

2 Likes

Recall that .now() is a method whose only job it is to retrieve the datetime properties from the system. The system clock is in milliseconds and like time itself, always going forward. Successive calls are always going to be different.

1 Like