Python - convert number of hours to a date? [duplicate] - python

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

Related

Convert a number sequence into a date using python? (example: 221201) [duplicate]

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

How to extract hours, minutes and secods from datetime.fromtimestamp [duplicate]

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)

How do I remove hours and seconds from my DataFrame column in python? [duplicate]

This question already has answers here:
How to change the datetime format in Pandas
(8 answers)
Keep only date part when using pandas.to_datetime
(13 answers)
Closed 10 months ago.
I have a DataFrame :
Age Gender Address Date
15 M 172 ST 2022-02-07 00:00:00
I Want to remove hh:mm:ss
I tried:
import datetime as dt
df["Date"]=df["Date"].dt.Date .
But I am receiving no change in date column format.
All I want is that the date column has only (YYYY-MM-DD).
You can use pd.to_datetime to convert Date column to datetime object.
df['Date'] = pd.to_datetime(df['Date']).dt.date
# or
df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%Y-%m-%d')
# or
df['Date'] = df['Date'].str.split(' ').str[0]

Converting datetime to date (Difference in Integer or Number format) [duplicate]

This question already has answers here:
Python: Convert timedelta to int in a dataframe
(6 answers)
Closed 1 year ago.
I have multiple columns with datetime format "14-09-2021 12:00:00 AM". I have converted them to date with the below code.
df['Column1'] = pd.to_datetime(df['Column1']).dt.date
df['Column2'] = pd.to_datetime(df['Column2']).dt.date
Intention is to take the difference between the two columns to identify the difference in days. When I take the difference df['diff']=df['Column1']-df['Column2'] I get the result as say "- 39 days" and not jus "- 39" (Image added below for clarity)
Intention is to get the difference in integer or number format (i.e just 39 and not 39 days), I'm not sure on how to go about this or when I have erred in the above code.
Help would be much appreciated.
Try this df['diff'] = df['diff'].astype(int)

Python convert integer to date [duplicate]

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'

Categories