I have a small issue with the TimeSpan class where it can parse 23:59 but not 24:00.
Of course the client wants to enter 24:00 to indicate the end of the day rather than 23:59 or 00:00 as 00:00 indicates the start of the day.
Currently my code parses the end time like so:
if ( !TimeSpan.TryParse( ( gvr.FindControl( "txtTimeOff" ) as TextBox ).Text, out tsTimeOff ) )
{
this.ShowValidationError( String.Format( "Please enter a valid 'Time Off' on row '{0}'.", gvr.RowIndex + 1 ) );
return false;
}
Whats the best work around for this situation?
EDIT: (Solution 1)
if ( ( gvr.FindControl( "txtTimeOff" ) as TextBox ).Text == "24:00" )
{
tsTimeOff = new TimeSpan( 24, 0, 0 );
}
else
{
if ( !TimeSpan.TryParse( ( gvr.FindControl( "txtTimeOff" ) as TextBox ).Text, out tsTimeOff ) )
{
this.ShowValidationError(
String.Format( "Please enter a valid 'Time Off' on row '{0}'.", gvr.RowIndex + 1 ) );
return false;
}
}
EDIT: (solution2)
string timeOff = ( gvr.FindControl( "txtTimeOff" ) as TextBox ).Text;
if ( !TimeSpan.TryParse(
timeOff == "24:00" ? "1.00:00:00" : timeOff
, out tsTimeOff ) )
{
this.ShowValidationError(
String.Format( "Please enter a valid 'Time Off' on row '{0}'.", gvr.RowIndex + 1 ) );
return false;
}