How to produce “human readable” strings to represent a TimeSpan

To get rid of the complex if and switch constructs you can use a Dictionary lookup for the correct format string based on TotalSeconds and a CustomFormatter to format the supplied Timespan accordingly. public string GetReadableTimespan(TimeSpan ts) { // formats and its cutoffs based on totalseconds var cutoff = new SortedList<long, string> { {59, “{3:S}” … Read more

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

How to get a Timespan of 1 year?

The tricky thing is that what a year is, depends on where it starts. You can do DateTime now = DateTime.Now; TimeSpan span = now.AddYears(1) – now; This would give you the 1 year timespan from the current moment to one year later

TimeSpan FromMilliseconds strange implementation?

The first consideration is wondering why they selected a double as the return value. Using long would have been an obvious choice. Although there already is a perfectly good property that is long, Ticks is unambiguous with a unit of 100 nanoseconds. But they picked double, probably with the intention to return a fractional value. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)