Click here to Skip to main content
14,363,648 members
Rate this:
Please Sign up or sign in to vote.
See more:
I tried to subscribe tow datetime
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 4: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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Use a proper format template in your ToString call, such as:
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]
   
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.
Golden Basim 12-Feb-15 9:42am
   
Input string was not in a correct format.
Richard MacCutchan 12-Feb-15 9:54am
   
What string?
Golden Basim 12-Feb-15 9:59am
   
the error appear here : label11.Text = DEF.ToString("hh:mm:ss");
Richard MacCutchan 12-Feb-15 10:12am
   
See my modified code.
Golden Basim 12-Feb-15 10:23am
   
yes , it work .

thank you
Rate this:
Please Sign up or sign in to vote.

Solution 3

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:

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
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.

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;
   
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.
Richard MacCutchan 12-Feb-15 9:56am
   
You still have not read the documentation properly. The extra field in the OP's question is explained there.
Golden Basim 12-Feb-15 10:24am
   
Thanks a lot
Ramza360 12-Feb-15 10:31am
   
no problem.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100