C# 两个日期之间的天、小时、分钟、秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10538507/
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
Days, hours, minutes, seconds between two dates
提问by Sachin Kainth
I have two dates, one less than the other. I want to create a string such as this one
我有两个约会,一个比另一个少。我想创建一个这样的字符串
"0 days, 0 hours, 23 minutes, 18 seconds"
“0 天 0 小时 23 分 18 秒”
representing the difference between the two dates. How can I get these elements of this string?
表示两个日期之间的差异。我怎样才能得到这个字符串的这些元素?
采纳答案by Alexei Levenkov
回答by bdukes
When you subtract one DateTimefrom another, you get a TimeSpaninstance, which exposes those values.
当你DateTime从另一个中减去一个时,你会得到一个TimeSpan实例,它公开这些值。
TimeSpan diff = DateTime.Now - DateTime.Today;
string formatted = string.Format(
CultureInfo.CurrentCulture,
"{0} days, {1} hours, {2} minutes, {3} seconds",
diff.Days,
diff.Hours,
diff.Minutes,
diff.Seconds);
回答by General Grey
Have you tried using
你有没有试过使用
TimeSpan()
that can certainly do what you want
那当然可以做你想做的
回答by Shawn
Use a TimeSpan
使用时间跨度
DateTime startTime = DateTime.Now;
DateTime endTime = DateTime.Now.AddSeconds( 75 );
TimeSpan span = endTime.Subtract ( startTime );
Console.WriteLine( "Time Difference (seconds): " + span.Seconds );
Console.WriteLine( "Time Difference (minutes): " + span.Minutes );
Console.WriteLine( "Time Difference (hours): " + span.Hours );
Console.WriteLine( "Time Difference (days): " + span.Days );
String yourString = string.Format("{0} days, {1} hours, {2} minues, {3} seconds",
span.Days, span.Hours, span.Minutes, span.Seconds);
回答by CodeHxr
How about something like this?
这样的事情怎么样?
TimeSpan diff = dateTimeNew - dateTimeOld;
string output = string.Format("{0} days, {1} hours, {2} minues, {3} seconds", diff.Days, diff.Hours, diff.Minutes, diff.Seconds);
Console.WriteLine(output);
回答by Habib
DateTime myDay = DateTime.Now;
DateTime otherDate = DateTime.Now.AddYears(1);
var test = otherDate.Subtract(myDay);
Console.WriteLine("Days:" + test.Days + "Hours:" + test.Hours +"Minutes" + test.Minutes +"Seconds" + test.Seconds);
Here test is of type TimeStamp
这里的测试类型为 TimeStamp
回答by Mark Brackett
Use the TimeSpanclass, which you'll get when you subtract the dates.
使用TimeSpan类,您将在减去日期时获得该类。
You can format the output using standardor customformat strings.
"0 days, 0 hours, 23 minutes, 18 seconds"
“0 天 0 小时 23 分 18 秒”
can be had with something like:
可以有类似的东西:
TimeSpan ts = DateTime.Now - DateTime.Today;
Console.WriteLine(
string.Format("{0:%d} days, {0:%h} hours, {0:%m} minutes, {0:%s} seconds", ts)
);
IMO, it's cleaner and easier to use string.Formatinstead of having to escape the words in your format string (which you'd need if you just used .ToString) or building it up manually.
IMO,它更干净,更易于使用,string.Format而不必转义格式字符串中的单词(如果您刚刚使用,则需要.ToString)或手动构建它。
回答by Vinay
TimeSpan diffTime = dateTimeNew -PreviousDate;
int days=diffTime.Days;
int hours=diffTime.Hours;
int minutes=diffTime.Minutes;
int seconds=diffTime.Seconds;
回答by ComeIn
Don't forget that if you want this calculation to be portable you need to store it as UTC and then when you display it convert to local time. As a general rule Store dates as UTC and convert to local time for presentation.
不要忘记,如果您希望此计算可移植,则需要将其存储为 UTC,然后在显示时将其转换为本地时间。作为一般规则,将日期存储为 UTC 并转换为本地时间以进行演示。

