This question already has answers here:
Converting unix timestamp string to readable date
(19 answers)
Closed 5 months ago.
from response i get date time stamp like this '1663935188183'. I used python datetime.fromtimestamp() function and print date in full format '2022-09-23 14:13:08.183000'
My question is. Can i extract from function above just hours, minutes and secods ?
You can try this solution
>> from datetime import datetime
# Be carefule this will raise ValueError: year 54698 is out of range
# you have to divide timestamp by 1e3 if your timestamp is in milliseconds
>> timestamp = 1663935188183/1e3
>> date = datetime.fromtimestamp(timestamp)
>> date.hour, date.minute, date.second
(14, 13, 8)
Related
This question already has answers here:
How can I convert a string into a date object and get year, month and day separately?
(4 answers)
Closed 2 months ago.
I have a bunch of number sequences like "221201" meaning the year 2022, month 12 and day 01 (so 2022-12-01). How can I convert number sequences in this format into the actual date using python?
I've tried using the dateutil library but couldn't figure out how to get it to recognize this format.
from datetime import datetime
date_str = '221201'
date_obj = datetime.strptime(date_str, '%y%m%d')
print(type(date_obj))
print(date_obj) # printed in default format
You can learn more about strptime in this link
This question already has answers here:
Pandas converting row with unix timestamp (in milliseconds) to datetime
(4 answers)
Closed 1 year ago.
i would like to convert unix timestamp to utc time. i have to convert it using the code below, but each time i get an incorrect result. online date converter returns correct result. how to convert a date to count the number of hours between two periods?
import datetime
print(datetime.datetime.fromtimestamp(1633438809404))
ValueError: year 53731 is out of range
import time
epoch = 1633438809404
time_obj = time.localtime(int(epoch))
print(time_obj)
time.struct_time(tm_year=53731, tm_mon=8, tm_mday=13, tm_hour=18, tm_min=36, tm_sec=44, tm_wday=0, tm_yday=225, tm_isdst=0)
import pandas
timestamp = 1633438809404
unix_conversion=pandas.to_datetime(timestamp, unit='ns')
print(str(unix_conversion))
1970-01-01 00:27:13.438809404
The timestamp you've provided (1633438809404) is a timestamp in milliseconds, yes? If so, then divide it by 1000 before providing it to datetime.datetime.fromtimestamp (which expects a timestamp in seconds):
>>> datetime.fromtimestamp(1633438809404/1000)
datetime.datetime(2021, 10, 5, 9, 0, 9, 404000)
or, when using pandas.to_datetime, ensure you use unit='ms':
>>> timestamp = 1633438809404
>>> unix_conversion=pandas.to_datetime(timestamp, unit='ms')
>>> unix_conversion
Timestamp('2021-10-05 13:00:09.404000')
This question already has answers here:
Difference between two dates in Python
(7 answers)
Closed 2 years ago.
I would like to convert a date into int value, but the int should be the difference of the date and another date that I specifed. To be more clear, let assume that I want to convert the date 15/03/2020 in the int difference between it and the base date, that for example is 02/02/2020. In this case I would obtain 41 as result, cause it is the difference in days between the 2 dates. How can I do it in python?
You might subtract datetime.date from datetime.date to get datetime.timedelta i.e.
import datetime
d1 = datetime.date(2020, 3, 15)
d0 = datetime.date(2020, 2, 2)
diff = (d1-d0).days
print(diff)
Output:
42
This question already has answers here:
Converting unix timestamp string to readable date
(19 answers)
Closed 2 years ago.
I have in Python 3.7 a date in form of an integer that represents the number of hours from 1/1/1900 00 Hours. Can I transform it into string of format dd/mm/yyyy?
for example:
timenumber = 1043148
timestring = magictrickfunctions(timenumber)
print(timestring)
should give "01/01/2017"
Are you sure it gives you 01/01/2017 instead of 01/01/2019?
To obtain the number of days divide your total hours by /24 this will give you the number of days. You can then specify that as shown below. I am using pandas library here.
import pandas as pd
start_date = "01/01/1900"
date_1 = pd.to_datetime(start_date)
end_date = date_1 + pd.DateOffset(days=43464) #specify the number of days and add it to your start_date
print(end_date)
Outtput
2019-01-01 00:00:00
This question already has answers here:
How to convert a given ordinal number (from Excel) to a date
(5 answers)
Closed 7 years ago.
I have this list of integers and need to convert it to date using python.
42222 should be 8/6/2015
42290 should be 10/13/2015
42319 should be 11/11/2015
I get the equal date of the integer when i paste in to excel then format the cell to Date.
Excel dates start counting around the year 1900. This will do it:
from datetime import datetime, timedelta
def xldate_to_datetime(xldate):
tempDate = datetime(1900, 1, 1)
deltaDays =timedelta(days=int(xldate)-2)
TheTime = (tempDate + deltaDays )
return TheTime.strftime("%m/%d/%Y")
>>> xldate_to_datetime(42290)
'10/13/2015'