👻 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?
1 answers
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.
Answer #1
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:
- Italiano Convert python datetime to epoch with strftime
- Deutsch Convert python datetime to epoch with strftime
- Français Convert python datetime to epoch with strftime
- Español Convert python datetime to epoch with strftime
- Türk Convert python datetime to epoch with strftime
- Русский Convert python datetime to epoch with strftime
- Português Convert python datetime to epoch with strftime
- Polski Convert python datetime to epoch with strftime
- Nederlandse Convert python datetime to epoch with strftime
- 中文 Convert python datetime to epoch with strftime
- 한국어 Convert python datetime to epoch with strftime
- 日本語 Convert python datetime to epoch with strftime
- हिन्दी Convert python datetime to epoch with strftime
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
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
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