# Convert python datetime to epoch with strftime

| | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

I have a time in UTC from which I want the number of seconds since epoch.

I am using strftime to convert it to the number of seconds. Taking 1st April 2012 as an example.

``````>>>datetime.datetime(2012,04,01,0,0).strftime("%s")
"1333234800"
``````

1st of April 2012 UTC from epoch is 1333238400 but this above returns 1333234800 which is different by 1 hour.

So it looks like that strftime is taking my system time into account and applies a timezone shift somewhere. I thought datetime was purely naive?

How can I get around that? If possible avoiding to import other libraries unless standard. (I have portability concerns).

👻 Read also: what is the best laptop for engineering students?

How do I calculate the date six months from the current date using the datetime Python module?

By Eef

I am using the datetime Python module. I am looking to calculate the date 6 months from the current date. Could someone give me a little help doing this?

The reason I want to generate a date 6 months from the current date is to produce a review date. If the user enters data into the system it will have a review date of 6 months from the date they entered the data.

478

I found this solution to be good. (This uses the python-dateutil extension)

``````from datetime import date
from dateutil.relativedelta import relativedelta

six_months = date.today() + relativedelta(months=+6)
``````

The advantage of this approach is that it takes care of issues with 28, 30, 31 days etc. This becomes very useful in handling business rules and scenarios (say invoice generation etc.)

``````\$ date(2010,12,31)+relativedelta(months=+1)
datetime.date(2011, 1, 31)

\$ date(2010,12,31)+relativedelta(months=+2)
datetime.date(2011, 2, 28)
``````

We hope this article has helped you to resolve the problem. Apart from Convert python datetime to epoch with strftime, check other code Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:

Julia Ungerschaft

Vigrinia | 2023-01-27

I was preparing for my coding interview, thanks for clarifying this - Convert python datetime to epoch with strftime in Python is not the simplest one. Will use it in my bachelor thesis

Carlo Jackson

Singapore | 2023-01-27

Thanks for explaining! I was stuck with Convert python datetime to epoch with strftime for some hours, finally got it done 🤗. I am just not quite sure it is the best method

Manuel Galleotti

Abu Dhabi | 2023-01-27

imp Python module is always a bit confusing 😭 Convert python datetime to epoch with strftime is not the only problem I encountered. Will use it in my bachelor thesis

## Shop

Learn programming in R: courses

\$FREE

Best Python online courses for 2022

\$FREE

Best laptop for Fortnite

\$399+

Best laptop for Excel

\$

Best laptop for Solidworks

\$399+

Best laptop for Roblox

\$399+

Best computer for crypto mining

\$499+

Best laptop for Sims 4

\$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

PythonStackOverflow

Check if one list is a subset of another in Python

PythonStackOverflow

How to specify multiple return types using type-hints

PythonStackOverflow

Printing words vertically in Python

PythonStackOverflow

Python Extract words from a given string

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

PythonStackOverflow

Python os.path.join () method

PythonStackOverflow

Flake8: Ignore specific warning for entire file

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries