-2

Hi guys i have a problem with this code that i found... i have 2 datetimepickers and i would like to find the difference in days between both of the 2 datetimepickers.

 textBox1.Text = ((dateTimePicker2.Value - dateTimePicker1.Value)).TotalDays.ToString("#");

I cannot convert the textbox to a string nor a int...I do not understand what does the ToString("#") means too......... The results from the codes are wierd too... Result 1

Result 2

NoobCoder
  • 27
  • 5
  • https://stackoverflow.com/questions/2330302/days-difference-between-two-dates – Mohamed Najiullah Nov 27 '17 at 04:28
  • I don't know what ToString("#") means either. Why is it in there? What's wrong with just ToString()? – Sean O'Neil Nov 27 '17 at 04:40
  • ToString("#") converts to something but i am not sure what it means.... Definitely not the same as ToString(); – NoobCoder Nov 27 '17 at 04:43
  • I'm not familiar with the # parameter but regardless why are you using it and what happens when you don't? – Sean O'Neil Nov 27 '17 at 04:46
  • The `#` character is a [custom numeric format string](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings), which specifies the number of digits to display. Non-significant zeros are not displayed, which is why you see nothing when the number of days difference is zero. – Rufus L Nov 27 '17 at 05:30
  • What is the actual problem you're having, though? The output you show looks correct to me (aside from the fact that there is no `0` displayed for the first picture, but that's easily fixed by removing the `#` altogether or replacing it with `0`). – Rufus L Nov 27 '17 at 05:42
  • Just wanted to know what is the # means... can you guys stop downvoting its hurting my feelings:( – NoobCoder Nov 27 '17 at 06:57

1 Answers1

0

To find the difference in days between 2 datetimepickers values, you can try this code also,

TimeSpan tSpan = dateTimePicker2.Value - dateTimePicker1.Value;
textBox1.Text = tSpan.TotalDays.ToString("#");

# in the string format represents the number of significant digits to display. When it is after the decimal place, the resulting number will be rounded to the number of digits. For example:

 5.67.ToString("#"); 

your result will be: 6

In the case of

5.67.ToString("#.#")

your result will be: 5.7

and in the case of

5.67.ToString("#.##")

your result will be: 5.67

Abhilash Ravindran C K
  • 1,818
  • 2
  • 13
  • 22