javascript 时间跨度删除秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5111484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TimeSpan remove seconds
提问by Maya
How do you truncate the seconds bit from a timespan object in C#? i.e. 15:37
如何从 C# 中的时间跨度对象中截断秒位?即 15:37
I'm outputting a timespan object to JavaScript in the format of HH:mm and kind of want the server side to process providing the correct format instead of clients browsers, can that be done without providing this as a C# string object to JavaScript?
我正在以 HH:mm 的格式向 JavaScript 输出一个时间跨度对象,并且希望服务器端处理提供正确的格式而不是客户端浏览器,是否可以在不将其作为 C# 字符串对象提供给 JavaScript 的情况下完成?
回答by Fredrik M?rk
You can use a format string for that:
您可以为此使用格式字符串:
public string GetTimeSpanAsString(TimeSpan input)
{
return input.ToString(@"hh\:mm");
}
回答by Will Dean
You can truncate the 'ticks' value which is the core of a TimeSpan:
您可以截断作为 TimeSpan 核心的“刻度”值:
TimeSpan t1 = TimeSpan.FromHours(1.551);
Console.WriteLine(t1);
TimeSpan t2 = new TimeSpan(t1.Ticks - (t1.Ticks % 600000000));
Console.WriteLine(t2);
Gives:
给出:
01:33:03.6000000
01:33:00
回答by xmedeko
Maybe not optimal, but easy to read:
也许不是最佳的,但易于阅读:
TimeSpan.FromMinutes((long)duration.TotalMinutes);
回答by The Muffin Man
I believe this is what you're looking for.
我相信这就是你要找的。
string.Format("{0:H:mm}",myTime)
回答by CodesInChaos
Perhaps something like this. This truncates to minutes using the truncation of an integer division, followed by a multiplication by the divisor.
也许是这样的。这使用整数除法的截断截断为分钟,然后与除数相乘。
return TimeSpan.FromTicks(input.Ticks/TicksPerMinute*TicksPerMinute);

