Question

I have a TimeSpan variable which has the current hour.

TimeSpan  time = new TimeSpan(DateTime.Now.Hour, 0, 0);

I'm trying to format to the current device Time format. For that I take current format like this and then try to format the time variable

string modified = CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern.ToString();
str = time.ToString(modified);

And this throws error: Modified contains {hh:mm:ss tt}

How do I do this?

Était-ce utile?

La solution

Its very simple, Just use this line of code.

DateTime dt = DateTime.Now.Date;
DateTime dtfinal = dt.Date + time;
string modified = CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern.ToString();
string check = dtfinal.ToString(modified);
DisplayTimeTB.Text = check;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top