Coder
Coder

Reputation: 455

Why time.mktime and datetime.timestamp generate different timestamps with the same date string input?

I am trying to convert the date string to timestamp but through implementing the following ways, I get different results:

import time
import datetime

date = "11/10/2020"

# First way
print(time.mktime(time.strptime(date, "%d/%m/%Y")))

# Second way
print(datetime.datetime.timestamp(datetime.datetime.strptime(date, "%m/%d/%Y")))

Output:

1602370800.0
1604966400.0

Why don't they generate the same outputs?

Upvotes: 0

Views: 176

Answers (1)

sahasrara62
sahasrara62

Reputation: 11228

you need to process the date/month/year in the same format in both cases to achieve the same result, as in the first case you are taking it as %d/%m/%Y while in case 2 you are processing it as %m/%d/%Y. this is making the whole difference

make the processing format the same and you will see the same result

Upvotes: 1

Related Questions