简体   繁体   English

将时间格式00:44:50转换为('00:44:50.240000')

[英]convert time format 00:44:50 To ('00:44:50.240000')

            string val;
            val = Request.Form["timerData"].ToString();            
            TimeSpan ts = TimeSpan.FromMilliseconds(Convert.ToInt32(val.ToString()));
            lbTime.Text = ts.ToString();
  • Here lbTime.Text returns me the time value in 00:44:50 format. 在这里,lbTime.Text以00:44:50格式返回我的时间值。

  • In my Sql Database I have used Time(7) datatype for time and its 在我的Sql数据库中,我已将Time(7)数据类型用于时间及其
    Default value is ('00:00:00.0000000') 默认值为('00:00:00.0000000')

  • I want to convert this format 00:44:50 To ('00:44:50.240000') and enter it in database using update statement. 我想将此格式从00:44:50转换为('00:44:50.240000'),然后使用更新语句将其输入数据库。

Time Above is for Example Purpose 以上时间仅供参考

Please help me with the same. 请同样帮我。

If your update statement is parameterized or you use a stored procedure, passing a TimeSpan to a time(7) column should work perfectly fine . 如果将update语句参数化或使用存储过程,则将TimeSpan传递给time(7)列应该可以正常工作 Something like this: 像这样:

public static void UpdateTimestamp( int id , TimeSpan ts )
{
  using ( SqlConnection connection = new SqlConnection( "some-connect-string" ) )
  using ( SqlCommand    command    = connection.CreateCommand() )
  {
    command.CommandText = "update foo set duration = @duration where id = @id" ;

    command.Parameters.AddWithValue( "@id"       , id ) ;
    command.Parameters.AddWithValue( "@duration" , ts ) ;

    connection.Open() ;
    int rowsAffected = command.ExecuteNonQuery() ;
    connection.Close() ;

    if ( rowsAffected == 0 ) throw new InvalidOperationException( "That didn't work B^(" ) ;
    if ( rowsAffected >  0 ) throw new InvalidOperationException( "That shouldn't have happend B^(" ) ;

  }

  return ;

}

Try one of these: 尝试以下方法之一:

DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15, 18);
CultureInfo ci = CultureInfo.InvariantCulture;

Console.WriteLine(date1.ToString("hh:mm:ss.f", ci));
// Displays 07:27:15.0
Console.WriteLine(date1.ToString("hh:mm:ss.F", ci));
// Displays 07:27:15
Console.WriteLine(date1.ToString("hh:mm:ss.ff", ci));
// Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.FF", ci));
// Displays 07:27:15.01
Console.WriteLine(date1.ToString("hh:mm:ss.fff", ci));
// Displays 07:27:15.018
Console.WriteLine(date1.ToString("hh:mm:ss.FFF", ci));
// Displays 07:27:15.018

You can even use more 'FFFFF's in order to represents up to seven most significant digits of the seconds fraction. 您甚至可以使用更多的'FFFFF'来表示秒分数的最多七个最高有效数字。

Taken from: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx 摘自: http : //msdn.microsoft.com/zh-cn/library/8kb3ddd4.aspx

Does it helps? 有帮助吗?

In your last expression you say 在最后一个表情中,您说

ts.ToString()

This overload only displays fractional seconds if not all digits are zero. 如果并非所有数字均为零,则此重载仅显示小数秒。 You may want to use a custom format string, as in 您可能需要使用自定义格式字符串,例如

ts.ToString(@"hh\:mm\:ss\.fffffff")

instead. 代替。 This ought to work in .NET 4.5 and 4.0. 这应该在.NET 4.5和4.0中起作用。 See MSDN: Custom TimeSpan Format Strings . 请参阅MSDN:自定义TimeSpan格式字符串

I used the same code only mistake was i was converting value to int before passing it to timespan 我使用相同的代码唯一的错误是我在将值传递给timepan之前将值转换为int

string val;
            val = Request.Form["timerData"].ToString();            
            TimeSpan ts = TimeSpan.FromMilliseconds(Convert.ToDouble(val.ToString()));
            lbTime.Text = ts.ToString();

Solved 解决了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM