This question already has answers here:
Convert a column of yyyy-mm-dd datetimes to linux time epoch in Python
(3 answers)
Closed 7 months ago.
I have been trying to convert the date and time given in following format "2021/12/04 11:10:00.000" to UNIX time.I have attached the sample of my program I tried. It does provide me the UNIX time but when I checked for the converted UNIX time it didn't gave me the correct UNIX time conversion.
for i in dx['date']:
year= i[0:4]
month = i[5:7]
day = i[8:10]
hr = i[11:13]
minute = i[14:16]
sec = i[17:19]
time = year+','+month+','+day+','+hr+','+minute+','+sec
print("Debug", time)
unixtime = datetime.datetime(int(year),int(month),int(day),int(hr),int(minute),int(sec)).timestamp()
Convert string datetime to datetime obj, and then retrieve timestamp.
from datetime import datetime
t = "2021/12/04 11:10:00.000"
dt = datetime.strptime(t, "%Y/%m/%d %H:%M:%S.%f")
dt.timestamp()
Output:
1638637800.0
Related
This question already has answers here:
Python UTC datetime object's ISO format doesn't include Z (Zulu or Zero offset)
(13 answers)
Closed 3 months ago.
from datetime import datetime
import pytz
# local datetime to ISO Datetime
iso_date = datetime.now().replace(microsecond=0).isoformat()
print('ISO Datetime:', iso_date)
This doesn't give me the required format i want
2022-05-18T13:43:13
I wanted to get the time like '2022-12-01T09:13:45Z'
The time format that you want is known as Zulu time format, the following code changes UTC to Zulu format.
Example 1
import datetime
now = datetime.datetime.now(datetime.timezone.utc)
print(now)
Output
#2022-12-01 10:07:06.552326+00:00
Example 2 (Hack)
import datetime
now = datetime.datetime.now(datetime.timezone.utc)
now = now.strftime('%Y-%m-%dT%H:%M:%S')+ now.strftime('.%f')[:4] + 'Z'
print(now)
Output
#2022-12-01T10:06:41.122Z
Hope this helps. Happy Coding :)
You can use datime's strftime function i.e.
current_datetime = datetime.now().replace(microsecond=0)
print(f'ISO Datetime: {current_datetime.strftime("%Y-%m-%dT%H:%M:%SZ")}')
This question already has answers here:
How do I translate an ISO 8601 datetime string into a Python datetime object? [duplicate]
(11 answers)
Closed 4 months ago.
I am writing a UI automated test that checks a date in the database and returns the date as a string in this format 1975-07-14T16:32:47.000Z and comparing it to the date that is displayed on the webpage but the date on the webpage is in this format Day-Month name-Year (14 July 1975), therefore I need to convert the date return by the database to Day-Month name-Year (14 July 1975) so that I am comparing like for like. How do I change the date string to the format I need
You can use dateutil.parser to parse the string you got from the datebase into a datetime.datetime, which in turn can be formatted using strftime:
import dateutil.parser
input="1975-07-14T16:32:47.000Z"
dt = dateutil.parser.parse(input)
print(dt.strftime("%d %B %Y"))
from datetime import datetime
dt_string = "1975-07-14T16:32:47.000Z"
datetime_object = datetime.strptime(dt_string, "%Y-%m-%dT%H:%M:%S.%fZ")
new_datetime_string = datetime.strftime(datetime_object, "%d-%B-%Y")
print(new_datetime_string)
# prints "14-July-1975"
We are using datetime module where datetime.strptime will generate a datetime object where you can call .date(),.time(),.today() and other functions but to get back to string as per the given format of Day-Month Name-Year datetime.strftime()(stringify time) is used. This converts datetime obj to given format of datetime string.
%d - date (DD - 01,02,...,31)
%m - month (MM - 01,02,...,12)
%Y - Year (YYYY - 2022,2021,...)
%B - Full Month Name (January, Feburary,..)
%f - Milliseconds
you can find out more in following link: Datetime format codes
This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 2 years ago.
I need to convert HH:MM:SS format to time.time() object. Is there any way to do it?
Here's my HH:MM:SS format time:
a = '14:37:29'
I want to convert it to time.time() object such as:
a = 1600256249
Is this achievable? If it's not, what should I try? Hope you help.
To get UNIX time, you need to add a date. For example, you could combine your time string with today's date:
from datetime import datetime, timezone
s = '14:37:29'
today = datetime.today() # 2020-09-16
# make a datetime object with today's date
dt = datetime.combine(today, datetime.strptime(s, '%H:%M:%S').time())
# make sure it's in UTC (optional)
dt = dt.replace(tzinfo=timezone.utc)
# get the timestamp
ts = dt.timestamp()
print(ts)
# 1600267049.0
You could also set other time zones with this approach using dateutil or zoneinfo (Python 3.9+).
Is this achievable?
I would say no. a = '14:37:29' holds only hour-minute-second, whilst time.time() does return seconds since start of epoch i.e. you would also need to known day, month and year beside hour, minute, second, to create equivalent of what time.time() returns.
This question already has answers here:
How can I convert a datetime object to milliseconds since epoch (unix time) in Python?
(14 answers)
Closed 3 years ago.
I want to convert current datetime into Unix time stamp
My Code
import time
import datetime
d = datetime.datetime.now()
unixtime = time.mktime(d.timetuple())
print(unixtime)
My output:
1577098747.0
Expected Output:
1577098747123.0
Above code gives me timestamp upto 10 digits but I want it to be accurate till 13 digits.
Note: I don't want to convert it manually multiplying by 10**3 I want to capture accurate milliseconds.
do it like this
import time
import datetime
d = datetime.datetime.now()
unixtime = datetime.datetime.timestamp(d)*1000
print(unixtime)
or you just use time.time()
This question already has answers here:
Adding days to a date in Python
(16 answers)
Closed 2 years ago.
I have the following date format:
year/month/day
In my task, I have to add only 1 day to this date. For example:
date = '2004/03/30'
function(date)
>'2004/03/31'
How can I do this?
You need the datetime module from the standard library. Load the date string via strptime(), use timedelta to add a day, then use strftime() to dump the date back to a string:
>>> from datetime import datetime, timedelta
>>> s = '2004/03/30'
>>> date = datetime.strptime(s, "%Y/%m/%d")
>>> modified_date = date + timedelta(days=1)
>>> datetime.strftime(modified_date, "%Y/%m/%d")
'2004/03/31'