Posts

Showing posts with the label TimeSpan

Donate

Data Annotation Validation Of TimeSpan Property In Model Using RegularExpression Attribute

Hello, To validate a model property of type TimeSpan using Data Annotation RegularExpression attribute, the expressions should check the hours, minutes and seconds since the corresponding database column in SQL Server that is mapped with this field has a Time data type. The example below validates a 23 hour format military time and does not allow minutes or seconds greater than zero. [RegularExpression(@"^(?:[01][0-9]|2[0-3]):[0-0][0-0]:[0-0][0-0]$ ", ErrorMessage =" Invalid time format and hh:mm:ss values. ")]

Get Total Hours And Minutes From Total Minutes Using TimeSpan Class In C# And VB.NET

Image
Good evening! Given total number of minutes as input and you need to extract the total number of hours and remaining minutes out from the input, you need to use the TimeSpan class specifically FromMinutes() method. Based from the docs ,the FromMinutes method returns a TimeSpan object that represents a specified number of minutes, where the specification is accurate to the nearest millisecond. C# Code 1 2 3 4 TimeSpan span = TimeSpan.FromMinutes(1506); var hours = ( int ) span.TotalHours; var minutes = span.Minutes; Console.WriteLine(hours + ":" + minutes.ToString( "D2" )); VB.NET Code 1 2 3 4 Dim span As TimeSpan = TimeSpan.FromMinutes(1506) Dim hours = CInt (span.TotalHours) Dim minutes = span.Minutes Console.WriteLine(hours + ":" + minutes.ToString( "D2" )) Output

Donate