Can you round a .NET TimeSpan object?

Sorry, guys, but both the question and the popular answer so far are wrong 🙂 The question is wrong because Tyndall asks for a way to round but shows an example of truncation. Will Dean’s answer is wrong because it also addresses truncation rather than rounding. (I suppose one could argue the answer is right … Read more

Work with a time span in Javascript

Sounds like you need moment.js e.g. moment().subtract(‘days’, 6).calendar(); => last Sunday at 8:23 PM moment().startOf(‘hour’).fromNow(); => 26 minutes ago Edit: Pure JS date diff calculation: var date1 = new Date(“7/Nov/2012 20:30:00”); var date2 = new Date(“20/Nov/2012 19:15:00”); var diff = date2.getTime() – date1.getTime(); var days = Math.floor(diff / (1000 * 60 * 60 * 24)); … Read more

How to Convert string “07:35” (HH:MM) to TimeSpan

While correct that this will work: TimeSpan time = TimeSpan.Parse(“07:35”); And if you are using it for validation… TimeSpan time; if (!TimeSpan.TryParse(“07:35”, out time)) { // handle validation error } Consider that TimeSpan is primarily intended to work with elapsed time, rather than time-of-day. It will accept values larger than 24 hours, and will accept … Read more

C# 4.0: Can I use a TimeSpan as an optional parameter with a default value?

You can work around this very easily by changing your signature. void Foo(TimeSpan? span = null) { if (span == null) { span = TimeSpan.FromSeconds(2); } … } I should elaborate – the reason those expressions in your example are not compile-time constants is because at compile time, the compiler can’t simply execute TimeSpan.FromSeconds(2.0) and … Read more

Why does TimeSpan.ParseExact not work

From the documentation: Any other unescaped character in a format string, including a white-space character, is interpreted as a custom format specifier. In most cases, the presence of any other unescaped character results in a FormatException. There are two ways to include a literal character in a format string: Enclose it in single quotation marks … Read more

What is the easiest way to subtract time in C#?

These can all be done with DateTime.Add(TimeSpan) since it supports positive and negative timespans. DateTime original = new DateTime(year, month, day, 8, 0, 0); DateTime updated = original.Add(new TimeSpan(5,0,0)); DateTime original = new DateTime(year, month, day, 17, 0, 0); DateTime updated = original.Add(new TimeSpan(-2,0,0)); DateTime original = new DateTime(year, month, day, 17, 30, 0); DateTime … Read more

Format A TimeSpan With Years

A TimeSpan doesn’t have a sensible concept of “years” because it depends on the start and end point. (Months is similar – how many months are there in 29 days? Well, it depends…) To give a shameless plug, my Noda Time project makes this really simple though: using System; using NodaTime; public class Test { … Read more

How do I convert a TimeSpan to a formatted string? [duplicate]

I just built a few TimeSpan Extension methods. Thought I could share: public static string ToReadableAgeString(this TimeSpan span) { return string.Format(“{0:0}”, span.Days / 365.25); } public static string ToReadableString(this TimeSpan span) { string formatted = string.Format(“{0}{1}{2}{3}”, span.Duration().Days > 0 ? string.Format(“{0:0} day{1}, “, span.Days, span.Days == 1 ? string.Empty : “s”) : string.Empty, span.Duration().Hours > … Read more

Environment.TickCount vs DateTime.Now

Environment.TickCount is based on GetTickCount() WinAPI function. It’s in milliseconds But the actual precision of it is about 15.6 ms. So you can’t measure shorter time intervals (or you’ll get 0) Note: The returned value is Int32, so this counter rolls over each ~49.7 days. You shouldn’t use it to measure such long intervals. DateTime.Ticks … Read more