Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将hh:mm格式的时间跨度值获取为字符串变量_C#_Datetime_Timespan - Fatal编程技术网

C# 将hh:mm格式的时间跨度值获取为字符串变量

C# 将hh:mm格式的时间跨度值获取为字符串变量,c#,datetime,timespan,C#,Datetime,Timespan,我以hh:mm:ss格式获取值。但我只想获取hh:mm格式的值。首先,您的代码甚至不会编译。字符串需要使用双引号,而不是单引号 DateTime date1 = Convert.ToDateTime('2015/06/20'); DateTime date2= Convert.ToDateTime('2015/05/20'); TimeSpan latetime = date1.Subtract(date2);//here in 'hh:mm:ss' format string value=la

我以
hh:mm:ss
格式获取值。但我只想获取
hh:mm
格式的值。首先,您的代码甚至不会编译。字符串需要使用双引号,而不是单引号

DateTime date1 = Convert.ToDateTime('2015/06/20');
DateTime date2= Convert.ToDateTime('2015/05/20');
TimeSpan latetime = date1.Subtract(date2);//here in 'hh:mm:ss' format
string value=latetime.ToString();
顺便说一下,您在
TimeSpan latetime=date1.Subtract(date2)上看到的(格式)行可能只是一个调试器表示形式。
TimeSpan
本身没有任何隐式格式。仅当您尝试获取其文本表示时,格式化概念才是一个问题

TimeSpan
格式设置与
DateTime
格式设置略有不同。您可以使用
hh\\\:mm
格式,如

DateTime date1 = Convert.ToDateTime("2015/06/20");
DateTime date2 = Convert.ToDateTime("2015/05/20");
或者您可以使用逐字字符串文字

string value = latetime.ToString("hh\\:mm");
但结果将是
00:00
如果您需要天,则:

string value=latetime.ToString("hh\\:mm");

您请求的可能是总小时:分钟格式的字符串:

string value=latetime.ToString("dd\\:hh\\:mm");

这将返回:
744:00

?我认为OP不希望结果是
744:00
。谁知道呢?这个问题的题目很清楚。
string value=latetime.ToString("dd\\:hh\\:mm");
string value=((int)latetime.TotalHours).ToString() + ":" + latetime.ToString("mm");