TimeSpan.ToString(“hh:mm”) error [duplicate]
DateTime.Now.TimeOfDay.ToString(@”hh\:mm\:ss”) Documentation
DateTime.Now.TimeOfDay.ToString(@”hh\:mm\:ss”) Documentation
Isn’t there a TimeSpan.Duration method? I think this would handle what you are trying to do. Returns a new TimeSpan object whose value is the absolute value of the current TimeSpan object.
You need to convert your data to TimeSpan and then use format:”hh\:mm” string test =”08:00:00″; TimeSpan ts = TimeSpan.Parse(test); Console.Write(ts.ToString(@”hh\:mm”)); In your case: var test = dataRow.Field<TimeSpan>(“fstart”).ToString(@”hh\:mm”)); Remember to escape the colon : You may see: Custom TimeSpan Format Strings
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
Although currently a tick is 100 nanoseconds, it is best not to rely on this as an absolute. Rather, use ‘TimeSpan.TicksPerSecond‘ (or any of the other TicksPerXxx member values)
Do you want calculate the age in years for an employee? Then you can use this snippet (from Calculate age in C#): DateTime now = DateTime.Today; int age = now.Year – bday.Year; if (bday > now.AddYears(-age)) age–; If not, then please specify. I’m having a hard time understanding what you want.
With JDK 8 date-time libraries in SDK has been enriched and you can use Duration or Period Interval from JodaTime will do.. A time interval represents a period of time between two instants. Intervals are inclusive of the start instant and exclusive of the end. The end instant is always greater than or equal to … Read more
You can use the Average overload that takes a collection of long in parameter: double doubleAverageTicks = sourceList.Average(timeSpan => timeSpan.Ticks); long longAverageTicks = Convert.ToInt64(doubleAverageTicks); return new TimeSpan(longAverageTicks);
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
string startTime = “7:00 AM”; string endTime = “2:00 PM”; TimeSpan duration = DateTime.Parse(endTime).Subtract(DateTime.Parse(startTime)); Console.WriteLine(duration); Console.ReadKey(); Will output: 07:00:00. It also works if the user input military time: string startTime = “7:00”; string endTime = “14:00″; TimeSpan duration = DateTime.Parse(endTime).Subtract(DateTime.Parse(startTime)); Console.WriteLine(duration); Console.ReadKey(); Outputs: 07:00:00. To change the format: duration.ToString(@”hh\:mm”) More info at: http://msdn.microsoft.com/en-us/library/ee372287.aspx Addendum: Over … Read more