Last post Jun 23, 2020 06:37 AM by XuDong Peng
Member
144 Points
31 Posts
Jun 22, 2020 09:22 AM|Uncle Vince|LINK
Hi,
On MySQL table it's memorize this value on column sTime datatype time (HH:mm:ss)
sTime
24:20:34
This value is a time difference between two start and end dates
2020-06-19 23:11:10 Start date 2020-06-20 23:31:44 End date
For recovery this value 24:20:34 on aspx page I have inserted
tx2.Text = reader["sTime"].ToString().ToUpper();
But the return on the browser is
1.00:21:00
I have tried without success
DateTime HHMMSS = Convert.ToDateTime(reader["sTime"]); tx2.Text = HHMMSS.ToString("HH:mm:ss");
How to do resolve this?
Can you help me, please?
Contributor
2070 Points
659 Posts
Jun 23, 2020 06:37 AM|XuDong Peng|LINK
Hi Uncle Vince,
To calculate the time difference between two dates and times, many people use the TimeSpan Struct to solve it.
But if you use the TimeSpan Class, the part whose hour exceeds 23 will be converted to days by default.
Therefore, I think you have to use a string to display it. Please see the code below:
public static void Main() { DateTime dt = new DateTime(2020, 6, 19, 23, 11, 10); DateTime dt2 = new DateTime(2020, 6, 20, 23, 31, 44); TimeSpan ts = dt2.Subtract(dt); String TimeDiff = format(ts.Days * 24 + ts.Hours)+":"+format(ts.Minutes)+":"+format(ts.Seconds); Console.WriteLine(TimeDiff); //DateTime dateTime = Convert.ToDateTime(TimeDiff); //Console.WriteLine(dateTime); Console.ReadLine(); } public static String format(int data) { if (data < 10) return "0" + data.ToString(); return data.ToString(); }
Result:
Hope this can help you.
Best regards,
Xudong Peng
Member
144 Points
31 Posts
Datatype time MySQL vs aspx page in cs
Jun 22, 2020 09:22 AM|Uncle Vince|LINK
Hi,
On MySQL table it's memorize this value on column
sTime
datatype time (HH:mm:ss)This value is a time difference between two start and end dates
For recovery this value 24:20:34 on aspx page I have inserted
But the return on the browser is
I have tried without success
How to do resolve this?
Can you help me, please?
Contributor
2070 Points
659 Posts
Re: Datatype time MySQL vs aspx page in cs
Jun 23, 2020 06:37 AM|XuDong Peng|LINK
Hi Uncle Vince,
To calculate the time difference between two dates and times, many people use the TimeSpan Struct to solve it.
But if you use the TimeSpan Class, the part whose hour exceeds 23 will be converted to days by default.
Therefore, I think you have to use a string to display it. Please see the code below:
Result:
Hope this can help you.
Best regards,
Xudong Peng