Convert python datetime to epoch with strftime
If you want to convert a python datetime to seconds since epoch you could do it explicitly:
>>> (datetime.datetime(2012,4,1,0,0) - datetime.datetime(1970,1,1)).total_seconds()
1333238400.0
In Python 3.3+ you can use timestamp()
instead:
>>> datetime.datetime(2012,4,1,0,0).timestamp()
1333234800.0
Why you should not use datetime.strftime('%s')
Python doesn't actually support %s as an argument to strftime (if you check at http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior it's not in the list), the only reason it's working is because Python is passing the information to your system's strftime, which uses your local timezone.
>>> datetime.datetime(2012,04,01,0,0).strftime('%s')
'1333234800'
Python datetime to epoch
Try this:
t='20180515102500'
d=datetime.datetime.strptime(t, "%Y%m%d%H%M%S")
print(d)
epoch = datetime.datetime.utcfromtimestamp(0)
def unix_time_millis(dt):
return (dt - epoch).total_seconds() * 1000
unix_time_millis(d)
Let me know if that helps!
Convert date in words to epoch time stamp in python
With the input from the comment you could try:
from datetime import datetime
dates = {'Date': ['May 01, 2022', 'Apr 30, 2022', 'Apr 29, 2022', 'Apr 28, 2022', 'Apr 27, 2022']}
dates['Date'] = [
datetime.strptime(date, "%b %d, %Y").timestamp() for date in dates['Date']
]
Convert a set datetime to epoch time
I suggest you the following solution:
import datetime
today = datetime.datetime.now()
today_morning = today.replace(hour=0, minute=0, second=0, microsecond=0)
epoch = int(today_morning.timestamp())
print(epoch)
Converting Epoch time into the datetime
To convert your time value (float or int) to a formatted string, use:
import time
time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370))
For example:
import time
my_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370))
print(my_time)
How can I convert a datetime object to milliseconds since epoch (unix time) in Python?
It appears to me that the simplest way to do this is
import datetime
epoch = datetime.datetime.utcfromtimestamp(0)
def unix_time_millis(dt):
return (dt - epoch).total_seconds() * 1000.0
How to convert current date to epoch timestamp?
That should do it
import time
date_time = '29.08.2011 11:05:02'
pattern = '%d.%m.%Y %H:%M:%S'
epoch = int(time.mktime(time.strptime(date_time, pattern)))
print epoch
Related Topics
Problem Running Python from Crontab - "Invalid Python Installation"
Problems Adding Path and Calling External Program from Python
Tensorflow Install Fails with "Compiletime Version 3.5 of Module Does Not Match Runtime Version 3.6"
Set Bash Variable from Python Script
Unicodedecodeerror Reading Binary Input
What Is the Correct Way to Include Localisation in Python Packages
Python Script Not Working via Cron
Why Does Loading the Libc Shared Library Have "'Libraryloader' Object Is Not Callable" Error
Using Python Subprocess.Call() to Launch an Ncurses Process
How to Direct Output to a File When There Are Utf-8 Characters
How to Open Process Again in Linux Terminal
Converting a Pandas Groupby Output from Series to Dataframe
How to Hide the Console When I Use Os.System() or Subprocess.Call()
Docker.Errors.Dockerexception: Error While Fetching Server API Version