How to find the timestamp difference between two files [duplicate] - python

This question already has answers here:
How do I find the time difference between two datetime objects in python?
(21 answers)
Closed 3 years ago.
When I execute ll command I get the timestamps:
-rw-rw-r--+ 1 4167 May 5 17:19 file A
-rw-rw-r--+ 1 2721 May 4 17:08 file B
I want the difference between timestamps of A and B
I tried this:
datetime.fromtimestamp(getmtime(file)).strftime('%h %m %s'))
It gives
May 05 1557032395
May 04 1557084082
Please help me get the time difference

Looks like you need.
import os
import datetime
print(datetime.datetime.fromtimestamp(os.path.getmtime("file A")) - datetime.datetime.fromtimestamp(os.path.getmtime("file A")))
You can subtract 2 datetime objects to get the difference.

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

Convert column of Dataframe to time [duplicate]

This question already has answers here:
Convert number to time in Python
(3 answers)
Closed 2 months ago.
I have the following Dataframe:
Now i want to convert the column "ABZEIT" to time format. So the first rows would be:
13:05 15:40 14:20 16:30 07:40 12:05 17:15
A solution that first defines a to_time() function then apply it to the dataframe by using map().
from datetime import time
def to_time(t: int) -> time:
t = f"{t:04}" # add the leading zero if needed
return time(int(t[:2]), int(t[2:]))
df["ABZEIT"] = df["ABZEIT"].map(to_time)

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)

Remove "days 00:00:00"from dataframe [duplicate]

This question already has answers here:
Pandas Timedelta in Days
(5 answers)
Closed 3 years ago.
So, I have a pandas dataframe with a lot of variables including start/end date of loans.
I subtract these two in order to get their difference in days.
The result I get is of the type i.e. 349 days 00:00:00.
How can I keep only for example the number 349 from this column?
Check this format,
df['date'] = pd.to_timedelta(df['date'], errors='coerce').days
also, check .normalize() function in pandas.

Finding the day x years,months from a given time (Python) [duplicate]

This question already has answers here:
python getting weekday from an input date
(2 answers)
Closed 9 years ago.
Have a maths question here which I know to solve using only pen and paper. Takes a while with that approach, mind you. Does anybody know to do this using Python? I've done similar questions involving "dates" but none involving "days". Any of you folk able to figure this one out?
The date on 25/11/1998 is a Wednesday. What is the day on 29/08/2030?
Can anyone at least suggest an algorithm?
Cheers
Use the wonderful datetime module:
>>> import datetime
>>> mydate = datetime.datetime.strptime('29/08/2030', '%d/%M/%Y')
>>> print mydate.strftime('%A')
Tuesday
The algorithm/math is quite simple: There are always 7 days a week. Just calculate how many days between the two days, add it to the weekday of the given day then mod the sum by 7.
<!-- language: python -->
> from datetime import datetime
> given_day = datetime(1998,11,25)
> cal_day = datetime(2030,8,29)
> print cal_day.weekday()
3
> print (given_day.weekday() + (cal_day-given_day).days) % 7
3

Categories