How to calculate the Years and Months and Days in Date

Hello,

I have a date “10-01-1998” as dd-MM-yyyy format. I need to get this data

Years - 24 years
Months - 11 months
Days - 24 days

How do i do that?

Thanks in Advance

Hi @404_Error

Try this-

parsedDate = DateTime.ParseExact(givenDate, “dd-MM-yyyy”, CultureInfo.InvariantCulture)

Use the “Assign” activity to calculate the time span between the parsedDate and the current date. Subtract the parsedDate from the current date, which can be obtained using the DateTime.Now method. Assign the result to a variable, let’s call it timespan.

timeSpan = DateTime.Now - parsedDate

Thanks!!

Hey Thanks,

How can i get the Years, Months and Days from that?

Hi @404_Error

How about this expression

inputDate = DateTime.ParseExact("10-01-1998", "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture)

currentDate = DateTime.Now
  • Calculate the difference between the input date and the current date using the “Assign” activity. Create three variables: “yearsDiff”, “monthsDiff”, and “daysDiff”

Then use Assign:

yearsDiff = currentDate.Year - inputDate.Year
monthsDiff = currentDate.Month - inputDate.Month
daysDiff = currentDate.Day - inputDate.Day
  • Adjust the difference values based on any negative values or overflow in the months and days. You can use an “If” activity to handle this

Then Use If :

Condition: daysDiff < 0

 monthsDiff = monthsDiff - 1
 daysDiff = daysDiff + DateTime.DaysInMonth(currentDate.Year, currentDate.Month)

Use another If activity:

Condition: monthsDiff < 0

  yearsDiff = yearsDiff - 1
  monthsDiff = monthsDiff + 12

Use Message box activity

Message Box:
"You are " + yearsDiff.ToString + " years, " + monthsDiff.ToString + " months, and " + daysDiff.ToString + " days old."

Regards
Gokul

1 Like

Thank you so much for your help

I eliminated the negative values

but the values are different what i can see there

Thanks

Hi,

How did you get this value (24 years, 4 months, 23 days OR 9124 days)?

9124 days after Jan 10, 1998 is Jan 3, 2023, as the following.

image

Regards

Thanks mate, it works.
I Really Appreciate that.