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

前端 未结 11 1343
一生所求
一生所求 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;
                    }
    

提交回复
热议问题