Вопрос

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?

Это было полезно?

Решение

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;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top