-1

I have a conversion problem with this code:

Convert.ToString(result)
hourA.Text = result;

The result word is a DateTime and I would like to convert it in text to put it in a TextBox

For me, this code looks like good but I still have the following error:

CS0029 C# Cannot implicitly convert type to 'string'

What could I change to make it work?

3

2 Answers 2

1

you can use

hourA.Text = result.ToString("d");

to get short date time string representation, if your result hold value like 10/24/2019 10:54:40 AM the short text representation would be 10/24/2019,

there are too many string representation for datetime, all are listed in: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

Sign up to request clarification or add additional context in comments.

Comments

0

Convert datetime to string

DateTime date= DateTime.Now;
string result = date.ToString("dd-MM-yyyy hh:mm:ss tt");

append result string to textbox

hourA.Text = result ;

1 Comment

The result string is not appended to the Text property, it overwrites the value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.