0

The problem is the best way to compare DateTime form the past with now. I try to diff a past of time with DateTime now but It's not absolutely solve my problem. The function I have try is here =>

        public void diffOfEpochTime(DateTime past_time){
            double howlong = (DateTime.Now() - past_time).TotalDays;
            Console.write(howlong/365.25 + " Year " 
               + (howlong%365.25)/30 + " Month " 
               + (howlong%365.25)%30 + " Day");
        }

Seem like it's work but actually not because the diff of time is missed some of month and day.

Example the Time is January 1, 2018 12:00:00 AM and today is March 1, 2018 12:00:00 AM the answer should be 0 Year 2 Month 1 Day. but the Function's answer is 0 Year 1 month 29 Day

Anyone have a suggestion to me how to dare with this, Thank you.

s. srichomthong
  • 486
  • 5
  • 14
  • 3
    Why not `TimeSpan difference = d2 - d1;`? – ProgrammingLlama Sep 11 '18 at 09:15
  • 1
    Possible duplicate of [How do I get the time difference between two DateTime objects using C#?](https://stackoverflow.com/questions/2821040/how-do-i-get-the-time-difference-between-two-datetime-objects-using-c) – ProgrammingLlama Sep 11 '18 at 09:19
  • Is `Timespand` a typo of `TimeSpan`? Look at the [`TimeSpan` documentation](https://learn.microsoft.com/en-us/dotnet/api/system.timespan?view=netframework-4.7.2). it has an exemple of how to access Years, days etc.. – Drag and Drop Sep 11 '18 at 09:20
  • Sorry. I have a lot of version of this i'm very sorry I miss it.I have edit it now. so It's the last version of it. – s. srichomthong Sep 11 '18 at 09:23
  • Possible duplicate of [Difference between 2 days in years, months and days](https://stackoverflow.com/questions/27480886/difference-between-2-days-in-years-months-and-days) – Drag and Drop Sep 11 '18 at 09:31
  • @John, more about the complexe Timespan to Months and years than a Date diff. – Drag and Drop Sep 11 '18 at 09:33
  • @JaylerrInfinite, May you check Jon's answer https://stackoverflow.com/a/27480978/6560478. This is the simplier way. If you need to have a look on how Years can be complexe please read https://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c – Drag and Drop Sep 11 '18 at 09:36
  • "Why not TimeSpan difference = d2 - d1;? " Because I can get total Day from it but I also want month and Year too. Same with You Drag and Drop That answer is same with my function. – s. srichomthong Sep 11 '18 at 09:37
  • @ Drag and Drop Let I try. That might be the best way this problem. thank you. – s. srichomthong Sep 11 '18 at 09:40
  • Year is hard to define, Leap Second, Leap Year, Localisation because years start and end on different day base on where you are. It's really more complexe than simple division. And thats only for years. – Drag and Drop Sep 11 '18 at 09:41

1 Answers1

0

@Drag and Drop Your answer about Noda Time library is solve my problem => Another answer => Noda time

s. srichomthong
  • 486
  • 5
  • 14