How to calculate total number of days in between two dates in groovy [duplicate] - groovy

This question already has answers here:
Duration between two dates in Groovy
(2 answers)
Closed 9 years ago.
How to calculate total number of months and year in between two dates in groovy
date1 =2012 nov 1
date2 =2013 feb 1
required output is year = 0 and month =3.
Any answer is helpful

I think this should do:
monthBetween = (start[Calendar.MONTH] - end[Calendar.MONTH]) + 1
yearsBetween = start[Calendar.YEAR] - end[Calendar.YEAR]
months = monthBetween + (yearsBetween * 12)
Taken from Calculate difference in months between two dates with Groovy

Related

Current week in Python? [duplicate]

This question already has answers here:
Python: give start and end of week data from a given date
(5 answers)
how to get all dates of week based on week number in python
(7 answers)
Adding days to a date in Python
(16 answers)
Closed 9 months ago.
good morning, I'm in the middle of a project and I'm stuck on finding a way to return every day of the current week in a list. It would be something like this:
week=[ '2022-06-07' , '2022-06-08' , '2022-06-09' , '' , '...']
I would be very grateful if you could explain to me a way to obtain this result,,, thanks
You can shorten this but I wrote it out like this for clarity.
from datetime import datetime, timedelta
date_obj = datetime(2022, 6, 7) # Today, Tuesday
monday = date_obj - timedelta(days=date_obj.weekday()) # assuming start of week is Monday
week = []
for i in range(7):
week.append((monday + timedelta(days=i)).strftime("%Y-%m-%d"))
print(week)
Output:
# Monday 6/6 to Sunday 6/12
['2022-06-06', '2022-06-07', '2022-06-08', '2022-06-09', '2022-06-10', '2022-06-11', '2022-06-12']

how to convert date in month python? [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 3 years ago.
i m currently working in Generating dynamic invoice on monthly basis but i m stuck in getting month name in python from user input date
Example: Given Date is :15/03/2020 is date and month is March
Any help would be appreciated
Assuming that you are receiving the date in the form of a string like "15/03/2020":
int(date.split('/')[1]) returns the value between the first and second / of date as an integer. This can then be used as the index for a list of month names to return the name of the month:
months = ["January","February","March",...]
print(months[int(date.split('/')[1])-1])

How can I convert 23 years 0 months into numerical value so that I can use it in Predictive Modelling

I have a dataset in which values are in this form
23 years 0 months or
2 years 6 months
How can I convert it in numerical data or any other form so that it can be used in predictive modelling , Using pandas
Based on what ALollz said in the comments, you could split the line by spaces and add the months as a percentage of a year
a = "23 years 6 months"
b = a.split(" ")
print(str(float(b[0]) + float(b[2])/12))
Output: 23.5
I'm a fan of coding to let me easily change the output in the future so this is what I came up with:
convs = {'ye': 1, 'mo': 12, 'we': 52.1429, 'da': 365} # conversion rates to a year
conv_to = 'ye' # choose your desired unit!
times = {i: convs[conv_to] / float(convs[i]) for i in convs}
s = "1 year 1 month 1 week 1 day"
s = s.split(" ")
s = list(zip(s[0::2], s[1::2]))
converted_amt = sum([float(times[i[1].lower()[:2]]) * float(i[0]) for i in s])
print(converted_amt)
With that you can easily add more granularity to support hours minutes, and more.
Input:
conv_to = 'mo'
s = "23 years 2 months 3 weeks 24 days"
Output:
279.4794514873339
Input:
conv_to = 'ye'
s = "8 years 9 months 0 weeks 3 day"
Output:
3196.75
Input (works with any order):
conv_to = 'da'
s = "2 days 1 week"
Output:
8.999994246580073
As you can see it won't be totally accurate converting to days when weeks are used, as the value I use is an average found on Google. Rounding should help this.

In Excel, how do I calculate days between two dates ignoring full years?

How do I calculate relative years + days between two dates in excel? I want to know the number of years and any additional days (outside of the full years). Dividing the total days by 365 doesn't work because of leap years. For example:
Cell A1 = '1/31/2015'
Cell B1 = '2/1/2025'
Cell C1 = '2/11/2025'
=(B1-A1)/365 # 10.0109 = 10 years + 4 days, looking for 10 years + 0 days
=(C1-A1)/365 # 10.0383 = 10 years + 14 days, looking for 10 years + 10 days
Is there an easy way to calculate this?
Note - this question is not a duplicate of How to find the difference between dates in VBA -- which is a question about calculating the difference between dates. This question is asking how to calculate the difference of both years and days, such that the difference in days is for the final year only, and are not incorrectly including leap year days for previous years.
You can use the documented function DATEDIF
=DATEDIF(startdate,enddate,"Y")
This will give you the difference in whole years
“Y” Returns the period difference as complete years.
“M” Returns the period difference as complete months.
“D” Returns the number of days in the period.
“MD” Returns the difference between the days in ‘Start_Date’ and ‘End_Date’. Here the months and years of the dates are ignored.
“YM” Returns the difference between the months in ‘Start_Date’ and ‘End_Date’. Here the days and years of the dates are ignored
“YD” Returns the difference between the days of ‘Start_Date’ and ‘End_Date’. Here the years of the dates are ignored.
Excel Solution :
=DATEDIF(A1,A2,"y") & " years, " & DATEDIF(A1,A2,"ym") & " months, " & DATEDIF(A1,A2,"md") & " days"
Please refer this link

Sharepoint - End date minus Start date = Years?

I am needing to calculate the number of years between two fields;
Field A
Start Date (dd/mm/yyyy)
Field B
End Date (dd/mm/yyyy)
Field C
Calc formula display difference between Field A and B in years and months i.e 5.2
Thanks all.
Assuming you are asking about Calculated Columns then you need the DATEDIF function
Also I am assuming that the 5.2 is 5 years and 2 months - not 5 years and .2 of a year - be a bit more specific in your question and you will get better answers.
So
= DATEDIF([Start Date],[End Date],"Y")
& " years and " &
DATEDIF([Start Date],[End Date],"YM")
& " months "
Examples of common formulas

Resources