Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert time span value to format "hh:mm Am/Pm" using C#

Tags:

c#

.net

I have a value stored in variable of type System.TimeSpan as follows.

System.TimeSpan storedTime = 03:00:00; 

Can I re-store it in another variable of type String as follows?

String displayValue = "03:00 AM"; 

And if storedTime variable has the value of

storedTime = 16:00:00; 

then it should be converted to:

String displayValue = "04:00 PM"; 
like image 218
Amit Kumar Avatar asked Oct 24 '12 07:10

Amit Kumar


People also ask

How to format Time with am pm c#?

if you use "HH" -> The hour, using a 24-hour clock from 00 to 23. if you use "mm" -> The minute, from 00 through 59. if you use "m" - > The minute, from 0 through 59. if you add "tt" -> The Am/Pm designator.

How do you convert TimeSpan to hours?

A TimeSpan value can be represented as [-]d. hh:mm:ss. ff, where the optional minus sign indicates a negative time interval, the d component is days, hh is hours as measured on a 24-hour clock, mm is minutes, ss is seconds, and ff is fractions of a second. The value of the Hours property is the hours component, hh.


2 Answers

You can do this by adding your timespan to the date.

TimeSpan timespan = new TimeSpan(03,00,00); DateTime time = DateTime.Today.Add(timespan); string displayTime = time.ToString("hh:mm tt"); // It will give "03:00 AM" 
like image 80
Asif Mushtaq Avatar answered Sep 19 '22 08:09

Asif Mushtaq


Very simple by using the string format

on .ToSTring("") :

  • if you use "hh" ->> The hour, using a 12-hour clock from 01 to 12.

  • if you use "HH" ->> The hour, using a 24-hour clock from 00 to 23.

  • if you add "tt" ->> The Am/Pm designator.

exemple converting from 23:12 to 11:12 Pm :

DateTime d = new DateTime(1, 1, 1, 23, 12, 0); var res = d.ToString("hh:mm tt");   // this show  11:12 Pm var res2 = d.ToString("HH:mm");  // this show  23:12  Console.WriteLine(res); Console.WriteLine(res2);  Console.Read(); 

wait a second, there is a catch, the system Culture !!, the same code executed on windows set to different language especially with different culture language will generate different result.

for example in windows set to Arabic language the result Will be like this :

// 23:12 م

م means Evening (first letter of مساء) .

in windows set to German language i think it will show // 23:12 du.

you can change between different format on windows control panel under windows regional and language -> current format (combobox) and change... apply it, do a rebuild (execute) of your app and watch what i'm talking about.

so how can you force showing Am and Pm prefix in English event if the culture of the current system isn't set to English ?

easy just by adding two lines ->

the first step add using System.Globalization; on top of your code

and modify the previous code to be like this :

DateTime d = new DateTime(1, 1, 1, 23, 12, 0); var res = d.ToString("HH:mm tt", CultureInfo.InvariantCulture); // this show  11:12 Pm 

InvariantCulture => using default English Format.

another question I want to have the pm to be in Arabic or specific language, even if I use windows set to English (or other language) regional format?

Solution for Arabic Exemple :

DateTime d = new DateTime(1, 1, 1, 23, 12, 0); var res = d.ToString("HH:mm tt", CultureInfo.CreateSpecificCulture("ar-AE"));  

this will show // 23:12 م

event if my system is set to English region format. you can change "ar-AE" if you want to another language format. there is a list for each language.

exemples : ar ar-SA Arabic ar-BH ar-BH Arabic (Bahrain) ar-DZ ar-DZ Arabic (Algeria) ar-EG ar-EG Arabic (Egypt) .....

like image 24
Bilal Avatar answered Sep 21 '22 08:09

Bilal