转换时间跨度值格式化和QUOT; HH:MM AM / PM"使用C# [英] Convert time span value to format "hh:mm Am/Pm" using C#

查看:287
本文介绍了转换时间跨度值格式化和QUOT; HH:MM AM / PM"使用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有存储在类型变量的值 System.TimeSpan 如下:

  System.TimeSpan storedTime = 03:00:00;
 

我可以重新储存于类型的另一个变量字符串如下?

 字符串displayValue =上午03点00;
 

如果 storedTime 变量的值

  storedTime = 16:00:00;
 

那么就应该转换为:

 字符串displayValue =下午4点00分;
 

解决方案

您可以通过添加时间跨度的日期做到这一点。

 时间跨度时间跨度=新的时间跨度(03,00,00);
日期时间时间= DateTime.Today.Add(时间跨度);
字符串displayTime = time.ToString(HH:MM TT); //它会给上午03点00分
 

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

解决方案

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"

这篇关于转换时间跨度值格式化和QUOT; HH:MM AM / PM"使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆