English 中文(简体)
时间段周围的工作 2-4
原标题:Work around for TimeSpan parsing 24:00
  • 时间:2011-09-15 10:09:42
  •  标签:
  • c#
  • timespan

我在时间Span类别中有一个小问题,在那里,时间Span可以划分23:59,但不是24。

当然,客户希望进入24小时,以显示当天的结束,而不是23:59或00:00,即:00:00,即当天的开始。

目前,我的法典规定最终时间为:

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: (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;
}
最佳回答

试图这样做

textBox = (TextBox) gvr.FindControl ("txtTimeOff");

TimeSpan.TryParse (textBox.Text == "24:00"
                      ? "1.00:00"
                      : textBox.Text,
                   out tsTimeOff)
问题回答

该法典可帮助您:

string span = "35:15";
TimeSpan ts = new TimeSpan(int.Parse(span.Split( : )[0]),    // hours
                           int.Parse(span.Split( : )[1]),    // minutes
                           0);                               // seconds

http://stackoverflow.com/questions/2728321/how-to-parse-string-with-hours-greater-than-24-to-timespan/2728441#2728441" 如何用超过24小时的宽度 par平?

Generally the System.TimeSpan class is not well suited to represent a "point in time" but rather, as the name suggests, a "span" of time or duration. If you can refactor your code to use System.DateTime, or better yet System.DateTimeOffset, that would be the best solution. If that s not possible the other answer from Yahia is your best bet :)

最好的解决办法是通知客户,244小时不是有效小时,他应当使用0:00。 我从未见过从23:59跳跃到24。

但是,如果你坚持......解决以下涉及同一问题的SO问题,并包含一系列可能的解决办法:

C#24小时以上





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签