Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to subscribe tow datetime
C#
Publics.JSS_FT = null;
TimeSpan DEF = System.Convert.ToDateTime(dateTimePicker2.Text) - JCM_NOW_DT;
label11.Text = DEF.ToString();
Publics.JSS_FT = label11.Text;

but i get this result : 735140-15:25:55
during debuging : the value in DEF is good but the error here in label11.Text

how i can keep the format HH:mm:ss

thank you
Posted
Updated 12-Feb-15 3:18am
v2
Comments
Tomas Takac 12-Feb-15 9:22am    
Richard MacCutchan 12-Feb-15 9:49am    
Did you notice that your timespan is something over 2000 years? I suspect your calculation is the wrong way round, or JCM_NOW_DT is not a full date.
Golden Basim 12-Feb-15 9:52am    
string JCM_NOW = DateTime.Now.ToString("HH:mm:ss");
DateTime JCM_NOW_DT = Convert.ToDateTime(JCM_NOW);
Richard MacCutchan 12-Feb-15 10:16am    
Why do you want to convert a DateTime to a string just so that you can convert it back to a DateTime? Your datetimepicker most likely can give you a datetime value that you can use with DateTime.Now to get your timespan.

Use a proper format template in your ToString call, such as:
C#
label11.Text = DEF.ToString("hh\\:mm\\:ss");

Your code uses the default as described in the documentation[^], which is always the best place to check first.

[edit]
Added escape character to protect the colons.
[\edit]
 
Share this answer
 
v2
Comments
Ramza360 12-Feb-15 9:30am    
The default of a TimeSpan object ToString() produces the hh:mm:ss.
Richard MacCutchan 12-Feb-15 9:31am    
Go and read the documentation.
Ramza360 12-Feb-15 9:32am    
didn't have to go try it in code.
Richard MacCutchan 12-Feb-15 9:51am    
Doesn't matter, the documentation makes it clear why the extra value is in there.
Ramza360 12-Feb-15 9:53am    
Yes go read documentation. On the line Console.WriteLine(span), it just simply uses a TimeSpan object with no format specified, the output is 00:00:00.. Ergo using a timespan object .ToString() method alone gives the hh:mm:ss.......

And there are multiple ways of doing things, this is not the "wrong" way.
Thank you. i have solved the problem ..

I think that both of you are already true, but I did not know cause of the error so far

i Created a new form and rewrite the codes . the result is 00:15:12 is ok

the code:

C#
Publics.JSS_FT = null;
                            TimeSpan DEF = System.Convert.ToDateTime(dateTimePicker2.Text) - JCM_NOW_DT;
                            //  label11.Text = string.Format("{0:00}:{1:00}:{2:00}", DEF.Hours, DEF.Minutes, DEF.Seconds); // this is ok

                            // Or if by HH:mm:ss you mean TotalHours
                          //  label11.Text = string.Format("{0:00}:{1:00}:{2:00}", DEF.TotalHours, DEF.Minutes, DEF.Seconds); // this is ok
                            label11.Text = DEF.ToString(); // this is ok
 // label11.Text = DEF.ToString("hh:mm:ss"); // appear error  Input string was not in a correct format.
                            Publics.JSS_FT = label11.Text;


thanks
 
Share this answer
 
v2
The easiest way I know of formatting a string is using the string.Format method. But surprisingly, the DEF.ToString(); call (which is a TimeSpan object) does return the format hh:mm:ss, so not sure the issue is there.

C#
Publics.JSS_FT = null;
TimeSpan DEF = System.Convert.ToDateTime(dateTimePicker2.Text) - JCM_NOW_DT;
label11.Text = string.Format("{0:00}:{1:00}:{2:00}", DEF.Hours, DEF.Minutes, DEF.Seconds);

// Or if by HH:mm:ss you mean TotalHours
label11.Text = string.Format("{0:00}:{1:00}:{2:00}", DEF.TotalHours, DEF.Minutes, DEF.Seconds);

Publics.JSS_FT = label11.Text;
 
Share this answer
 
v4
Comments
Richard MacCutchan 12-Feb-15 9:31am    
Wrong, go and read the documentation, as I said above, for the correct way to do it.
Golden Basim 12-Feb-15 9:41am    
error appear :
in this line :
label11.Text = string.Format("{0:00}:{1:00}:{2:00)", DEF.Hours, DEF.Minutes, DEF.Seconds);

or in this line :
// Or if by HH:mm:ss you mean TotalHours
label11.Text = string.Format("{0:00}:{1:00}:{2:00)", DEF.TotalHours, DEF.Minutes, DEF.Seconds);

Input string was not in a correct format.
Ramza360 12-Feb-15 9:44am    
Sorry, spelling issue in the string.Format the 2:00 has a ) it should be a }.. Fixed in code
Richard MacCutchan 12-Feb-15 9:47am    
This is the wrong way to do it; use the proper fromat strings as shown in the documentation.
Ramza360 12-Feb-15 9:50am    
Yes go read documentation. On the line Console.WriteLine(span), it just simply uses a TimeSpan object with no format specified, the output is 00:00:00.. Ergo using a timespan object .ToString() method alone gives the hh:mm:ss.......

And there are multiple ways of doing things, this is not the "wrong" way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900