是否可以在c#
或javascript
中执行以下jquery
逻辑,代码将两小时之间的持续时间转换为小时,分钟和秒。持续时间20/01/2017 00:00:00至20/01/2017 02:20:30将是26:20:30
关于转换为 hh:mm:ss 格式小时的问题可以超过24小时,如果分钟数超过59小时,它应该添加到小时数和秒数超过59小时应添加到小时数。
public static string ToTimeFormat(bool includeSeconds = false)
{
var startDate = DateTime.parse("20/01/2017 00:00:00";
var endDate = DateTime.parse("20/01/2017 02:20:30");
var ts = endDate - startDate;
var totalDaysToHours = ts.Days * 24;
return string.Format("{0}:{1}", (ts.Hours + totalDaysToHours).ToString("0"),
ts.Minutes.ToString("D2"))
+ (includeSeconds ? ":" + ts.Seconds.ToString("D2") : string.Empty);
}
答案 0 :(得分:2)
您可以在此使用moment.js
moment.min.js,以下是代码
var startDate = "01/02/2016 00:00:00"; //MM/DD/YYYY HH:MM:SS format
var endDate = "01/03/2016 01:30:30"; //MM/DD/YYYY HH:MM:SS format
var diff = moment.duration(moment(endDate).diff(moment(startDate)));
var formatedData=[diff.asHours().toFixed(0), diff.minutes(), diff.seconds()].join(':');
所以你的输出就像
26:30:30
<强>更新强>
var startDate = "01/02/2016 00:00:00".split(/\//);
startDate= [ startDate[1], startDate[0], startDate[2] ].join('/'); // DD/MM/YYYY to MM/DD/YYYY
var endDate = "01/03/2016 01:30:30".split(/\//);
endDate = [ endDate[1], endDate[0], endDate[2] ].join('/'); // DD/MM/YYYY to MM/DD/YYYY
var diff = moment.duration(moment(endDate).diff(moment(startDate)));
var formatedData=[diff.asHours().toFixed(0), diff.minutes(),diff.seconds()].join(':');