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

前端 未结 11 1310
一生所求
一生所求 2020-11-28 07:22

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

System.TimeSpan storedTime = 03:00:00;

Can I re-store it

相关标签:
11条回答
  • 2020-11-28 07:53
    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 讨论(0)
  • 2020-11-28 07:54

    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 that is not all you need to care about something else is the system Culture because the same code executed on windows with other langage especialy with difrent culture langage will generate difrent result with the same code

    exemple of windows set to Arabic langage culture will show like that :

    // 23:12 م

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

    in another system culture depend on what is set on the windows regional and language option, 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 iam talking about.

    so who can I force showing Am and Pm Words 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 modifing 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?

    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")); 
    

    this will show // 23:12 م

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

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

    0 讨论(0)
  • 2020-11-28 07:58

    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"
    
    0 讨论(0)
  • 2020-11-28 07:58

    Doing some piggybacking off 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();
    }
    
    0 讨论(0)
  • 2020-11-28 07:58

    Parse timespan 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"
    
    0 讨论(0)
提交回复
热议问题