-2

In PHP, you can do this:

date_diff(date_create('2013-01-27 22:14:44 UTC'), date_create(date('Y-m-d H:i:s e')))->format('%y year(s), %m month(s), %d day(s), %H hour(s), %i minute(s) and %s second(s)')

to get the amount of years, days, months and so on since a date instead of just one of those. Is there a way to do this in c# too? Sorry for the short question

2 Answers2

2

In C#, Subtracting one DateTime object from another gives you a TimeSpan structure back. You'd use TimeSpan's .ToString("formathere") to get a nicely formatted String from it.

Edit: One issue you'll have is that TimeSpan doesn't have year or month format strings. You may want to use @JonSkeet's Noda Time instead, as seen here.

Community
  • 1
  • 1
Powerlord
  • 87,612
  • 17
  • 125
  • 175
1

It is alot simplier to do in c#, you can just use the subtraction operator on two DateTimes, the result of which is a TimeSpan which contains the number of days, hours, minutes, and seconds of the difference.

        DateTime date1 = new DateTime(2013, 01, 27, 22, 14, 44, 00, DateTimeKind.Utc);
        TimeSpan diff = date1 - DateTime.Now;

You can then format the output in a number of ways, see here for more format options.

        string output = String.Format("{0:dd} day(s), {0:hh} hour(s), {0:mm} minute(s) and {0:ss} second(s)", diff);
Jeffrey Wieder
  • 2,336
  • 1
  • 14
  • 12