0

To avoid label of duplicate here's a brief summary of all what i did.

After spending hours of googling to calculate the difference between two dates I came across 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 to extract the date in dd/mm/year and then insert it into database.To subtract the two dates using Period.Between(date1, date2, PeriodUnits.Years).Years how should i pass datetimepicker to it?

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

How should i get a complete rid of time while inserting in database as well as finding the difference while using datetimepicker instead of Datetime.

Update:

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

Giving the code datetimepicker1.value as 2/21/1990 (d_app) and datetimepicker2.value as 3/09/2015(d_ret) it returned 25 yrs 300months 9147 days.

What am i doing wrong?

Community
  • 1
  • 1
  • "but it doesn't work" doesn't tell us anything about what you're seeing - but that's clearly not valid code given that "DateTime datetimepicker.Days.ToString" isn't even slightly valid as a method argument. There's no need to use that other `Date` code, either. Why are you calling `Days`? Why are you referring to `ToString`? – Jon Skeet Mar 09 '15 at 09:24
  • Please note that using @JonSkeet for a question I haven't seen before doesn't do anything, btw. – Jon Skeet Mar 09 '15 at 09:28
  • It looks like you just want `var dateOfBirth = LocalDateTime.FromDateTime(dateTimePicker.Value).Date;` – Jon Skeet Mar 09 '15 at 09:28
  • Sorry if u mind for explicit mentioning but i bet u knew answer :) Using `Days.ToString` was the only meaningful option for the purpose provided by Intellisense. – yeung Leone Mar 09 '15 at 09:47
  • You misunderstood me - my point is that the explicit mention didn't do anything, because it wasn't in reply to something I'd written. – Jon Skeet Mar 09 '15 at 09:48
  • *Please* put more effort into formatting code in future. It makes a huge difference in readability. – Jon Skeet Mar 09 '15 at 10:25

1 Answers1

3

You're performing three separate computations here. You only need one:

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));
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Not going for `Date.cs` would i be able to get `mm/dd/year` using `NodaTime` for database insertion? – yeung Leone Mar 09 '15 at 10:56
  • @CrystalHeart: It's not clear what you mean, but you shouldn't be inserting into the database using *strings* at all. You'll probably want to convert back to `DateTime`, and set the parameter value in your SQL command that way. – Jon Skeet Mar 09 '15 at 10:58
  • What if i don't show the `time` and insert into database the date as string for `listview` things? – yeung Leone Mar 09 '15 at 11:21
  • Something like `Command.Parameters.Add("@appintment", SqlDbType.VarChar).Value = app_date.ToShortString();` where `ToShortString` were serving `mm/dd/yyyy` taken from `Date.cs` – yeung Leone Mar 09 '15 at 11:24
  • @CrystalHeart: I have no idea what you mean by that, but fundamentally if you're trying to store a date or a date/time value, you should make the database schema reflect that, and use the appropriate type of parameter when inserting. Why would you use varchar to represent a date? It's a recipe for disaster. – Jon Skeet Mar 09 '15 at 11:24
  • Do u consider it important for a date having time specified in a `listview` to user with `appointment`.If no,then how can i insert _only_ date in database – yeung Leone Mar 09 '15 at 11:27
  • @CrystalHeart: Sorry, your comments *really* don't make any sense to me. If you only want a date in the database, use a column type of Date. If you want a date and time, use DateTime. I don't see where list views come into that decision. It sounds like you should probably ask a new question though, because we're now a long way from your original question. – Jon Skeet Mar 09 '15 at 11:28
  • Using column of `Date` type to insert a date also adds a `12:00 AM` to each date which i want to get rid of.Now how would you avoid this ? – yeung Leone Mar 12 '15 at 08:06
  • @CrystalHeart: What do you mean by "adds a 12:00 AM to each date"? Do you mean when you fetch it as a `DateTime`? That's just because `DateTime` doesn't have the concept of "just a date". To avoid that, use `LocalDate` in Noda Time instead... – Jon Skeet Mar 12 '15 at 08:13
  • Would you mind giving a method of Noda Time which formats time in "mm/dd/yyyy"? – yeung Leone Mar 12 '15 at 09:45
  • @CrystalHeart: Have you read the user guide about text formatting? http://nodatime.org/1.3.x/userguide/text.html Then look at [`LocalDatePattern`](http://nodatime.org/1.3.x/api/html/AllMembers_T_NodaTime_Text_LocalDatePattern.htm) – Jon Skeet Mar 12 '15 at 09:48
  • @CrystalHeart: Sorry, I don't understand your comment at all. But at this point we're quite a long way from your original question. I suggest you ask a new question if you're stuck - complete with what you've tried, etc. – Jon Skeet Mar 12 '15 at 09:55
  • I just needed a method of noda time to replace `Date.cs` which Parses the date in "mm/dd/yyyy" format. – yeung Leone Mar 12 '15 at 09:55
  • @CrystalHeart: Right, so that's not the same as this question, which is about computing period differences - so ask a new question, after doing some research. – Jon Skeet Mar 12 '15 at 09:59