两个日期之间的天,小时,分钟,秒

时间:2021-08-15 21:27:15

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?

代表两个日期之间的差异。我怎样才能获得这个字符串的这些元素?

9 个解决方案

#1


73  

TimeSpan is the object you need:

TimeSpan是您需要的对象:

TimeSpan span = (DateTime.Now - DateTime.Now);

String.Format("{0} days, {1} hours, {2} minutes, {3} seconds", 
    span.Days, span.Hours, span.Minutes, span.Seconds);

#2


9  

When you subtract one DateTime from another, you get a TimeSpan instance, which exposes those values.

当您从另一个DateTime中减去一个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);

#3


3  

Use a TimeSpan

使用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);

#4


3  

Use the TimeSpan class, which you'll get when you subtract the dates.

使用TimeSpan类,当您减去日期时,您将获得该类。

You can format the output using standard or custom format 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.Format instead 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,则需要它)或手动构建它。

#5


2  

Have you tried using

你尝试过使用过吗?

TimeSpan()

that can certainly do what you want

那当然可以做你想要的

#6


2  

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

#7


2  

    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类型

#8


2  

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并转换为本地时间进行演示。

#9


1  

TimeSpan diffTime = dateTimeNew -PreviousDate;
int days=diffTime.Days;
int hours=diffTime.Hours;
int minutes=diffTime.Minutes;
int seconds=diffTime.Seconds;

#1


73  

TimeSpan is the object you need:

TimeSpan是您需要的对象:

TimeSpan span = (DateTime.Now - DateTime.Now);

String.Format("{0} days, {1} hours, {2} minutes, {3} seconds", 
    span.Days, span.Hours, span.Minutes, span.Seconds);

#2


9  

When you subtract one DateTime from another, you get a TimeSpan instance, which exposes those values.

当您从另一个DateTime中减去一个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);

#3


3  

Use a TimeSpan

使用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);

#4


3  

Use the TimeSpan class, which you'll get when you subtract the dates.

使用TimeSpan类,当您减去日期时,您将获得该类。

You can format the output using standard or custom format 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.Format instead 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,则需要它)或手动构建它。

#5


2  

Have you tried using

你尝试过使用过吗?

TimeSpan()

that can certainly do what you want

那当然可以做你想要的

#6


2  

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

#7


2  

    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类型

#8


2  

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并转换为本地时间进行演示。

#9


1  

TimeSpan diffTime = dateTimeNew -PreviousDate;
int days=diffTime.Days;
int hours=diffTime.Hours;
int minutes=diffTime.Minutes;
int seconds=diffTime.Seconds;