by
-1
6
4,873
19
Top 1% !
Famous
Easy-to-find
Specified
Tuned
OpenSource
Popularity: 1693rd place
.NETFramework4.5
Architecturex86_64
Extension.cs
InputName0FirstDay
InputName1LastDay
InputType0DateTime
InputType1DateTime
LanguageCSharp
MethodNameDateDiff
OutputTypestring

DateDiff - Return the Diferrence Between Two Dates

Returns a string in "X years, Y months and Z days" format
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/837542/DateDiff---Return-the-Diferrence-Between?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
public string DateDiff(DateTime FirstDay, DateTime LastDay) { TimeSpan diff = LastDay.Subtract(FirstDay); double years = Math.Floor(diff.Days / 365.25); double months = Math.Floor((diff.Days - (years * 365.25)) / 30); double days = Math.Floor(((diff.Days - (years * 365.25)) - (months * 30))); string year = (years > 0 ? (years == 1 ? years + " year," : years + " years,") : ""); string month = (months > 0 ? (months == 1 ? months + " month and" : months + " months and") : ""); string day = (days > 0 ? (days == 1 ? days + " day" : days + " days") : ""); return year + " " + month + " " + day; }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus