Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
1.80/5 (3 votes)
See more:
i am creating windows application here i am display a time on label, but how to convert label string value to int below code i am using but error is came input string was not in correct format.


lbltime.Text = DateTime.Now.ToString("hh tt");
 i = Convert.ToInt32(lbltime.Text);
lbltime.Text = i.ToString();



how to solve above error. any one please guide me.

What I have tried:

convert string to int that time error is came input string was not in correct format.
Posted
Updated 1-Feb-17 23:48pm
Comments
Karthik_Mahalingam 2-Feb-17 5:39am    
what is your expected output?
Boopalslm 2-Feb-17 5:43am    
how to start every day automatically reset in bill number starts from 1.
Karthik_Mahalingam 2-Feb-17 6:05am    
Always use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.
Karthik_Mahalingam 2-Feb-17 6:06am    
but this is not relevant to the question you have posted.
Boopalslm 2-Feb-17 6:31am    
k sir how to reset bill number every starts from one

Use the debugger to look at the string you return from ToString: "10 AM"
That isn't an integer. You could do it like this:
C#
lbltime.Text = DateTime.Now.ToString("HH");
i = Convert.ToInt32(lbltime.Text);
lbltime.Text = i.ToString();
And convert that, but you'd be better off using the Hour directly:
C#
lbltime.Text = DateTime.Now.Hour.ToString();
 
Share this answer
 
Comments
Boopalslm 2-Feb-17 5:39am    
Thanks sir it's working good, thanks a lot..........................
OriginalGriff 2-Feb-17 5:42am    
You're welcome!
That's not possible.
You can not convert a value which is in "hh tt".
The result of such format will be something like "11 AM", "02PM", etc.
If you just want to get "11" or "2" then you can do something like -
C#
i = Convert.ToInt32(lbltime.Text.ToSubstring(0,2));
lbltime.Text = i.ToString();


But looking at your code, you already are showing a value in format "hh tt", then why do you need to overwrite it immediately in the next line.
If you want to show the time in "hh tt" format, just remove the last 2 lines. Following should be enough.
C#
lbltime.Text = DateTime.Now.ToString("hh tt");


And if you want show just the hours, then something like following should help-
C#
lbltime.Text = DateTime.Now.ToString("hh");


Please let me know if your requirement is something else.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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