Converting a time interval value for formatting "hh: mm Am / Pm" using C #

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

 System.TimeSpan storedTime = 03:00:00; 

Is it possible to save it in another variable of type String as follows?

 String displayValue = "03:00 AM"; 

And if the variable storedTime matters

 storedTime = 16:00:00; 

then it should be converted to:

 String displayValue = "04:00 PM"; 
+54
c #
Oct 24 '12 at 7:28
source share
11 answers

You can do this by adding your time span 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" 
+118
Oct 24 '12 at 7:32
source share

ุจุณู… ุงู„ู„ู‡ ุงู„ุฑุญู…ุงู† ุงู„ุฑุญูŠู…

very simple using string format

on .ToSTring("") :

  • if you use "hh" โ†’> Hour, using a 12-hour clock from 01 to 12.

  • if you use "HH" โ†’> Hour, using a 24-hour clock from 00 to 23.

  • if you add "tt" โ†’> Designation Am / Pm.

conversion example 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, that not all you need to take care of something else is the Culture system, because the same code is executed in windows with a different langage especialy with a different culture langage will generate an excellent result with the same code

An example of windows installed in an Arabic language culture would look like this:

// 23: 12 ู…

ู… means Evening (first leter of ู…ุณุงุก).

in another system culture it depends on what is set on the regional and language options of Windows, it will display // 23:12 du.

you can change between different formats on the Windows control panel under regional and language versions of Windows โ†’ the current format (combobox) and change ... apply it, rebuild (execute) your application and see what iam is talking about.

so who can I make Am and Pm show in English if the culture of the current system is not installed in English?

easy by simply adding two lines: โ†’

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

and a modification of the previous code will look 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 the standard English format.

One more question: do I want pm to be in Arabic or a specific language, even if I use Windows installed in a regional (or other language) regional format?

Soution for Arabic Exemple:

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

it will show // 23:12 ู…

if my system is set to English. you can change "ar-AE" if you want to use a different format. There is a list of each language and its format.

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

...

Make me know that you have another question.

+13
Jul 23 '15 at 0:42
source share

You can add TimeSpan to DateTime , for example:

 TimeSpan span = TimeSpan.FromHours(16); DateTime time = DateTime.Today + span; String result = time.ToString("hh:mm tt"); 

Demo: http://ideone.com/veJ6tT

 04:00 PM 

Standard Date and Time Format Strings

+9
Oct 24 '12 at 7:30
source share

Fulfilling some answers to existing answers here:

 public static string ToShortTimeSafe(this TimeSpan timeSpan) { return new DateTime().Add(timeSpan).ToShortTimeString(); } public static string ToShortTimeSafe(this TimeSpan? timeSpan) { return timeSpan == null ? string.Empty : timeSpan.Value.ToShortTimeSafe(); } 
+9
Aug 15 '14 at 15:21
source share
 string displayValue="03:00 AM"; 

This is a point in time, not a duration (TimeSpan).

So, something is wrong with your basic design or assumptions.

If you want to use it, first you need to first convert it to a DateTime (point in time). You can format DateTime without the date part, which would be your search string.

 TimeSpan t1 = ...; DateTime d1 = DateTime.Today + t1; // any date will do string result = d1.ToString("hh:mm:ss tt"); 

The storeTime variable may have a value, for example, storeTime=16:00:00;

No, it can have a value of 4 hours, but the presentation is binary, TimeSpan cannot record the difference between 16:00 and 4 pm .

+7
Oct 24 '12 at 7:30
source share

You will need to get the DateTime object from your TimeSpan , and then you can easily format it.

One possible solution is to add time to any date with a time value of zero.

 var timespan = new TimeSpan(3, 0, 0); var output = new DateTime().Add(timespan).ToString("hh:mm tt"); 

The output value will be "03:00 AM" (for English).

+5
Oct 24 '12 at 7:33
source share

Parse the time to DateTime, and then use Format ("hh: mm: tt"). For example.

 TimeSpan ts = new TimeSpan(16, 00, 00); DateTime dtTemp = DateTime.ParseExact(ts.ToString(), "HH:mm:ss", CultureInfo.InvariantCulture); string str = dtTemp.ToString("hh:mm tt"); 

str will be:

 str = "04:00 PM" 
+2
Oct 24 '12 at 7:32
source share

First you need to convert the time interval to a DateTime structure:

 var dt = new DateTime(2000, 12, 1, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds) 

Then you need to convert the value to a string with Short Time format

 var result = dt.ToString("t"); // Convert to string using Short Time format 
0
Oct 24 '12 at 7:33
source share
 Parse timespan to DateTime. For Example. //The time will be "8.30 AM" or "10.00 PM" or any time like this format. public TimeSpan GetTimeSpanValue(string displayValue) { DateTime dateTime = DateTime.Now; if (displayValue.StartsWith("10") || displayValue.StartsWith("11") || displayValue.StartsWith("12")) dateTime = DateTime.ParseExact(displayValue, "hh:mm tt", CultureInfo.InvariantCulture); else dateTime = DateTime.ParseExact(displayValue, "h:mm tt", CultureInfo.InvariantCulture); return dateTime.TimeOfDay; } 
0
Jul 15 '15 at 9:30
source share

You can try the following:

  string timeexample= string.Format("{0:hh:mm:ss tt}", DateTime.Now); 

you can remove hh or mm or ss or tt according to your needs where hh hour at 12 h is formate, mm is minutes, ss is seconds, and tt is AM / PM.

0
Aug 6 '16 at 12:43 on
source share

You cannot add AM / PM to TimeSpan . In any case, you need to associate the TimaSpan value with DateTime if you want to display the time in a 12-hour clock format.

TimeSpan not intended for use with the 12-hour clock format, because here we are talking about the time interval .

As the documentation says;

A TimeSpan object represents a time interval (length of time or elapsed time) that is measured as a positive or negative number of days, hours, minutes, seconds and fractions of a second. The TimeSpan structure can also be used to represent the time of day, but only if the time is not related to a specific date. Otherwise, use the DateTime or DateTimeOffset structure instead.

Microsoft Docs also describe the following:

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

So, in this case, you can display using AM / PM as follows.

 TimeSpan storedTime = new TimeSpan(03,00,00); string displayValue = new DateTime().Add(storedTime).ToString("hh:mm tt"); 


Side note:
It should also be noted that the TimeOfDay DateTime property is a TimeSpan where it represents

a time interval that represents the fraction of the day that has passed since midnight.

0
Oct 19 '17 at 4:29 on
source share



All Articles