日期时间选择器的 NodaTime 用法

NodaTime usage for datetimepicker

为了避免重复标签,这里是我所做的所有工作的简要总结。

在谷歌上花费数小时计算两个日期之间的差异后,我发现 here and here where, i was convinced to use NodaTime to get difference in terms of years,months and days.My application needs accuracy to calculate pension.I used datetimepicker to get the date value from form and then i use Date.cs from here 提取 dd/mm/year 中的日期,然后将其插入 database.To 减去两个日期Period.Between(date1, date2, PeriodUnits.Years).Years 我应该如何将 datetimepicker 传递给它?

Here's Jon Skeet 所说:"you can use LocalDateTime.FromDateTime and then use the Date property to get a LocalDate".

我应该如何在插入数据库时​​完全摆脱时间以及在使用 datetimepicker 而不是 Datetime 时找到差异。

更新:

//Date of appointment
var d_app = LocalDateTime.FromDateTime(dateTimePicker1.Value).Date;
//Date of retirement
var d_ret = LocalDateTime.FromDateTime(dateTimePicker2.Value).Date;
var years=Period.Between(d_app,d_ret,PeriodUnits.Years).Years;
var months = Period.Between(d_app, d_ret, PeriodUnits.Months).Months;
var days = Period.Between(d_app, d_ret, PeriodUnits.Days).Days;
MessageBox.Show(years.ToString()+" years"+months.ToString()+"months "+days.ToString()+"days");

将代码 datetimepicker1.value 作为 2/21/1990 (d_app) 并将 datetimepicker2.value 作为 3/09/2015(d_ret) 返回 25 yrs 300months 9147 days.

我做错了什么?

您在此处执行三个单独的 计算。你只需要一个:

var appointment = LocalDateTime.FromDateTime(dateTimePicker1.Value).Date;
var retirement = LocalDateTime.FromDateTime(dateTimePicker2.Value).Date;
var difference = Period.Between(appointment, retirement);
MessageBox.Show(string.Format("{0} years {1} months {2} days",
   difference.Years, difference.Months, difference.Days));