Getting The Wrong Day From The Time Module

Hello Pythoneers,

I am currently using the time module to obtain the date since I use the date as part of the file name that I create. Today being the 28th, when I run the script I get the 29th. Can someone please verify if they obtain the same result. Here is a simple script to run to test the module:

from time import gmtime, strftime
print('The day of the month is: ', strftime("%d", gmtime()))

I am using Windows 11. I have tested this on both IDLE and PyCharm and I obtain the same results.

Here is a screenshot of the IDLE and PyCharm test results:

You’re using gmtime, which returns the time in UTC.

Are you in a timezone that’s several hours behind UTC?

1 Like

Shouldn’t the date reflect that which is on my system?

Edit:
I will instead use this one:

import datetime
current_time = datetime.datetime.now()

print(current_time.day)

If you wanted the local time you’d use localtime.

gmtime stands for “Greenwich Mean Time” and returns UTC, as I said.

Strictly speaking, Greenwich Mean Time isn’t the same thing as UTC, but the terms are often used interchangeably.

3 Likes

Yeah, the difference between GMT and UTC is that GMT never locked in any concept of leap seconds, so it’s slightly less precisely defined. As a general rule, you can consider “GMT” to be an archaic term meaning “UTC” and you’ll never be very far wrong.

2 Likes