How do I turn variable into converted time/date? [duplicate] - python

This question already has answers here:
How to convert milliseconds to date and time format?
(2 answers)
Closed 4 months ago.
I have a variable named "timestamp" and the value of it is "1617108899460", how would I turn this into this format: month/day/year, hour:minutes:seconds
I tried to do the following but I got an error:
date_time = datetime.fromtimestamp(timestamp).strftime("%A, %B %d, %Y %I:%M:%S")
OSError: [Errno 22] Invalid argument
Is there any way I can do this in a simple way? Like with a module?

Duplicate of this. Your timestamp value is in milliseconds.
from datetime import datetime
timestamp = 1617108899460
date_time = datetime.fromtimestamp(timestamp / 1e3).strftime("%A, %B %d, %Y %I:%M:%S")
This works as expected.

Related

Python convert time string with Z at end to datetime object [duplicate]

This question already has answers here:
How do I parse an ISO 8601-formatted date?
(29 answers)
How can I parse '2020-07-30T20:40:33.1000000Z' using datetime.strptime
(1 answer)
Closed 17 days ago.
This post was edited and submitted for review 17 days ago.
An API is providing a time stamp in the following format
s = "2023-02-02T21:05:07.2207121Z"`
I'm attempting to convert it to a datetime object
dt = datetime.strptime(s, "%Y-%m-%dT%H:%M:%S.%fZ")
It's causing the following error
line 349, in _strptime
raise ValueError("time data %r does not match format %r" %
ValueError: time data '2023-02-02T21:05:07.2207121Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'
This question was marked as a duplicated linked to a post suggesting fromisoformat(s), but that fails as well
dt = datetime.fromisoformat(s)
error
dt = datetime.fromisoformat(s)
ValueError: Invalid isoformat string: '2023-02-02T21:05:07.2207121Z'

How to convert a string with timezone to unix timestamp python? [duplicate]

This question already has answers here:
Convert timestamps with offset to datetime obj using strptime
(4 answers)
How do I parse an ISO 8601-formatted date?
(29 answers)
Closed 2 years ago.
I have a datetime string I get from a database and I want to convert it to unix timestamp.
I am not sure what is the way to do it.
db_timestamp = '2020-08-05 12:48:50+02:00'
f = '%Y-%m-%d %H:%M:%S%z'
timestamp = datetime.strptime(db_timestamp , f)
TypeError: strptime() argument 1 must be str, not datetime.datetime
Another way I tried was as following
python_timestamp = datetime.isoformat(db_timestamp)
test_timestamp = datetime.strptime(python_timestamp , f)
Then I get the following error
ValueError: time data '2020-08-05T12:48:50+02:00' does not match format '%Y-%m-%d %H:%M:%S%z'
How to fix this error?
What should be the correct string format for db_timestamp?
assuming you run Python 3.7 or higher, what you want is fromisoformat to parse the string and timestamp() to get seconds since the epoch UNIX time (POSIX).
from datetime import datetime
db_timestamp = '2020-08-05 12:48:50+02:00'
# to datetime object:
dt = datetime.fromisoformat(db_timestamp)
# to UNIX time:
ts = dt.timestamp()
print(repr(dt), ts)
>>> datetime.datetime(2020, 8, 5, 12, 48, 50, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200))) 1596624530.0

How to format time.time() using strftime() in python [duplicate]

This question already has answers here:
Convert timestamp since epoch to datetime.datetime
(3 answers)
Closed 3 years ago.
I have an epoch time that is in Seconds.PartOfSecond format e.g. 1581218900.17436. How can this be used within time.strftime() ?
import time
curTime = time.time()
# Following is not correct but gives idea what i'm looking for
formattedTime = time.strftime('%A %B %e, %Y %t', curTime)
Update The answer this has been closed for is not imo an obvious replacement: how would one know we need to convert to datetime.datetime? The answer below is more to the point.
You need pass your second to gmtime
import time
s=time.gmtime(1581218900.17436)
time.strftime("%Y-%m-%d %H:%M:%S", s)
'2020-02-09 03:28:20'
time.strftime('%A %B %e, %Y %t', s)
'Sunday February 9, 2020 \t'

Format email Date into "%Y-%m-%d %H:%M:%S" [duplicate]

This question already has answers here:
Convert timestamps with offset to datetime obj using strptime
(4 answers)
ISO to datetime object: 'z' is a bad directive [duplicate]
(2 answers)
Closed 3 years ago.
I'm trying to format the date of an email using python2.7. The value of date is like
Date: "Tue, 9 Apr 2019 11:49:26 -0400"
Here's the code:
"date": datetime.strptime(d_items['Date'], '%a,%d %b %Y %H:%M:%S %z').strftime('%Y-%m-%d %H:%M:%S')
While stripping the timezone and formatting I'm getting the below error using datetime.datetime function.
'z' is a bad directive in format '%a,%d %b %Y %H:%M:%S %z'
Is there any other way to get the format like below:
2019-04-09 11:49:26

How to convert a string to datetime object [duplicate]

This question already has answers here:
How do I parse an ISO 8601-formatted date?
(29 answers)
Closed 5 years ago.
With the time value being:
value = '2017-08-31T02:24:29.000Z'
I try to convert it to a datetime object with:
import datetime
datetime_object = datetime.datetime.strptime(value, '%Y-%b-%d %I:%M%p')
But the command crashes with the exception:
ValueError: time data '2017-08-31T02:24:29.000Z' does not match format '%Y-%b-%d %I:%M%p'
You should be using a builtin Python's datautil module instead of date time:
import dateutil.parser
value = '2017-08-31T02:24:29.000Z'
result = dateutil.parser.parse(value)
First of all, you are missing the formatter for the microsecond.
Second of all, there is no second colon for dividing the minute and second.
Third, the %b operator is for the monthname (Jan,Feb,etc.). You want to use %m.
Final format is '%Y-%m-%dT%I:%M:%S.%fZ'.
This is your code:
datetime_object = datetime.datetime.strptime(value, '%Y-%m-%dT%I:%M:%S.%fZ')
You should get 2017-08-31 02:24:29 as the value of datetime_object.

Categories