C# 两个日期时间之间的差异

C# 两个日期时间之间的差异,c#,wpf,datetime,C#,Wpf,Datetime,我有一个datetime,我想显示与datetime的区别。现在到接收的datetime并绑定它。结果应该是这样的: 1d 15h 13m 7s 最好的方法是什么字符串格式IValueConverter?使用 示例: DateTime oldDate = new DateTime(2002,7,15); DateTime newDate = DateTime.Now; // Difference in days, hours, and minutes. TimeSpan ts = newDa

我有一个datetime,我想显示与
datetime的区别。现在
到接收的datetime并绑定它。结果应该是这样的:

1d 15h 13m 7s
最好的方法是什么<代码>字符串格式<代码>IValueConverter?

使用

示例:

DateTime oldDate = new DateTime(2002,7,15);
DateTime newDate = DateTime.Now;

// Difference in days, hours, and minutes.
TimeSpan ts = newDate - oldDate;
// Difference in days.
int differenceInDays = ts.Days;
现在,您可以根据自己的要求进行更改。

使用

示例:

DateTime oldDate = new DateTime(2002,7,15);
DateTime newDate = DateTime.Now;

// Difference in days, hours, and minutes.
TimeSpan ts = newDate - oldDate;
// Difference in days.
int differenceInDays = ts.Days;

现在,您可以根据需要更改它。

我建议使用Timespans
ToString
方法和

如果您还没有意识到,时间跨度是为测量这样的时间间隔而设计的,可以通过从一个日期减去另一个日期来方便地获得

var startDate = new DateTime(2013,1,21);
var currentDate = DateTime.Now;
TimeSpan interval = currentDate - startDate;
string intervalInWords = String.Format("{0:%d} days {0:%h} hours {0:%m} minutes {0:%s} seconds", interval);
Console.WriteLine(intervalInWords);
这将打印出如下内容

267天10小时45分21秒


正如评论中所指出的,因为这些日期时间可能在不同的时区/夏令时,所以使用此技术时应该非常小心。如果可行的话,对这两种情况使用UTCtime(全年一致)就足够了。一般来说,最好的策略是将所有日期时间与时区/偏移量(如果需要)一起保存为UTC,然后在显示的特定时区偏移量中转换它们。

我建议使用Timespans
ToString
方法和

如果您还没有意识到,时间跨度是为测量这样的时间间隔而设计的,可以通过从一个日期减去另一个日期来方便地获得

var startDate = new DateTime(2013,1,21);
var currentDate = DateTime.Now;
TimeSpan interval = currentDate - startDate;
string intervalInWords = String.Format("{0:%d} days {0:%h} hours {0:%m} minutes {0:%s} seconds", interval);
Console.WriteLine(intervalInWords);
这将打印出如下内容

267天10小时45分21秒


正如评论中所指出的,因为这些日期时间可能在不同的时区/夏令时,所以使用此技术时应该非常小心。如果可行的话,对这两种情况使用UTCtime(全年一致)就足够了。一般来说,最好的策略是将所有日期时间与时区/偏移量(如果需要)一起保存为UTC,然后在特定的时区偏移量中需要时在屏幕上进行转换。

您可以使用TimeSpan,也可以查看[此处][1]

[1] 当前位置我建议您查看时间跨度

DateTime startDate = Convert.ToDateTime(2008,8,2);
    DateTime endDate = Convert.ToDateTime(2008,8,3);
    TimeSpan duration = startDate - endDate;

您可以使用TimeSpan,也可以查看[此处][1]

[1] 当前位置我建议您查看时间跨度

DateTime startDate = Convert.ToDateTime(2008,8,2);
    DateTime endDate = Convert.ToDateTime(2008,8,3);
    TimeSpan duration = startDate - endDate;

创建一个DateTime类型的属性,如DateProp,您将在XAML上绑定到该属性,并假设您的属性是Other\u date\u,则按如下方式初始化它:

1d 15h 13m 7s
DateProp=DateTime.Now.Subtract(此处为其他日期)

最后,在XAML上,将其绑定并设置如下格式:

1d 15h 13m 7s
Text=“{Binding Date,StringFormat=d天H小时m分s秒}”


(或您喜欢的任何其他格式:)。

创建一个DateTime类型的属性,如DateProp,您将在XAML上绑定到该属性,并假设您的属性在此处为other\u date\u,将其初始化如下:

1d 15h 13m 7s
DateProp=DateTime.Now.Subtract(此处为其他日期)

最后,在XAML上,将其绑定并设置如下格式:

1d 15h 13m 7s
Text=“{Binding Date,StringFormat=d天H小时m分s秒}”


(或您喜欢的任何其他格式:)。

从格式角度来看,其他答案都是正确的,但仅从WPF角度来看,我猜您希望更新标签/文本框,使其始终包含准确的持续时间

如果是这样,您可以使用计时器和调度程序来完成此操作

计时器代码:

//duration in milliseconds, 1000 is 1 second
var timer = new Timer(1000);
timer.Elapsed += timer_Elapsed;
timer.Start();
计时器运行代码:

//this is set elsewhere
private readonly DateTime _received;

void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    Application.Current.Dispatcher.Invoke(
        DispatcherPriority.Normal,
        new Action(() 
            => //replace label1 with the name of the control you wish to update
            label1.Content = 
            string.Format("{0:%d} days {0:%h} hours {0:%m} minutes {0:%s} seconds"
            , (DateTime.Now - _received))));
}

从格式的角度来看,其他答案都是正确的,但仅从WPF的角度来看,我猜您希望更新标签/文本框,使其始终包含准确的持续时间

如果是这样,您可以使用计时器和调度程序来完成此操作

计时器代码:

//duration in milliseconds, 1000 is 1 second
var timer = new Timer(1000);
timer.Elapsed += timer_Elapsed;
timer.Start();
计时器运行代码:

//this is set elsewhere
private readonly DateTime _received;

void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    Application.Current.Dispatcher.Invoke(
        DispatcherPriority.Normal,
        new Action(() 
            => //replace label1 with the name of the control you wish to update
            label1.Content = 
            string.Format("{0:%d} days {0:%h} hours {0:%m} minutes {0:%s} seconds"
            , (DateTime.Now - _received))));
}

问题标题说明了另外一件事。1)你试过什么?2)
DateTime.Now
不适用于此场景。问题标题说明了另外一件事。1)你试过什么?2)
DateTime.Now
不适用于此场景。伟大的我可以在mvvm中使用它吗?有没有办法隐藏0天0小时?我无法得到这个答案。我只有6个声誉。是的,你可以在MVVM中使用它,只要在实现INotifyPropertyChanged的模型类中将Timespan作为属性,在你的viewmodel中有这个类的一个实例,将这个viewmodel作为你视图的DataContext,然后将你的标签绑定到属性,如果你愿意,我可以更新我的答案?太好了。我可以在mvvm中使用它吗?有没有办法隐藏0天0小时?我无法得到这个答案。我只有6个声誉。是的,您可以在MVVM中使用它,只需将Timespan作为实现INotifyPropertyChanged的模型类中的属性,在viewmodel中拥有此类的实例,将此viewmodel作为视图的DataContext,然后将标签绑定到属性,如果您愿意,我可以更新我的答案?请注意,在世界上的许多地方,由于夏令时的原因,您的输出有一个额外的小时。除此之外,回答得好。@MattJohnson:这是一个很好的观点。请注意,在世界上许多地方,由于夏令时的原因,到今天为止,您的输出中有一个额外的小时。除此之外,回答得好。@MattJohnson:这是一个很好的观点。我要加一条注释说明这一点。