0

I'm trying to find the time between two dates, one being the current day (Today) and the other, a user defined deadline.

I'm working with C# Windows Forms and I've used a "date time picker" so that the user can choose a deadline date and I've created a string called Today and used

string Today = System.DateTime.Today.ToString("dd-mm-yyyy");

as the current date. But I don't know how to find the length of time between these two points (since they're strings), my program is a simple "to do list" where task duration's are in days and weeks (the "yyyy" is just for aesthetic purposes, it can be removed if necessary).

I've had a look over the internet and all I can seem to find is how to do this with "DateTime"s, instead of strings (or am I missing something?).

Any help would be greatly apreciated.

Raker
  • 13
  • 1
  • 6

4 Answers4

4

Use the 'Value' property of your DateTimePicker to get the DateTime value and use DateTime.Now to get the DateTime value for the current time (in the local timezone).

If you are only subtracting dates (with no time component), access the Date property of both DateTime objects before subtracting.

 DateTime userDate = dateTimePicker.Value.Date;
 DateTime currentDate = DateTime.Now.Date;
 TimeSpan difference = userDate.Subtract(currentDate);  //assuming deadline is in the future
roken
  • 3,946
  • 1
  • 19
  • 32
  • I've tried what you suggested but I'm not sure how I would display the Timespan, I've been playing around with the date, day and ToString() properties, but I can't seem to get anywhere. All that I got it to come up with was "00:00:00", I think this is because it's looking for minutes and hours and not getting any, because I'm sending it dates. Any ideas? – Raker Apr 29 '12 at 13:21
  • What are you looking to display? The total number of days or a more specific measurement? If you're only after the number of days, use the TotalDays property of the TimeSpan (see docs: http://msdn.microsoft.com/en-us/library/system.timespan.aspx). – roken Apr 29 '12 at 19:55
  • I've got it, I used the different properties of the Timespan to get what I was looking for. Thanks for your help. – Raker Apr 30 '12 at 12:11
3

Don't use two strings - use the actual DateTime instances. Strings don't and can't "understand" dates - that's why the DateTime object exists.

When you subtract two dates from each other, you get a TimeSpan instance. This gives you the amount of time difference.

TimeSpan difference = date1 - date2;
Oded
  • 489,969
  • 99
  • 883
  • 1,009
1

I guess the following you are looking for:

Date1.Subtract(Date2).TotalTime

Following link will help you understand more http://www.c-sharpcorner.com/UploadFile/DipalChoksi/DateDiff_CS_DC09132006172429PM/DateDiff_CS_DC.aspx

mannu2050
  • 403
  • 3
  • 11
0

AS Oded mentions the string is not the best approach. DateTimes cann be substracted from one another and give you a TimeSpan that represents, well, that, a time span.

Here is an MSDN example that should clarify things.

DateTime departure = new DateTime(2010, 6, 12, 18, 32, 0);
DateTime arrival = new DateTime(2010, 6, 13, 22, 47, 0);
TimeSpan travelTime = arrival - departure;  
Console.WriteLine("{0} - {1} = {2}", arrival, departure, travelTime);      
// The example displays the following output:
//       6/13/2010 10:47:00 PM - 6/12/2010 6:32:00 PM = 1.04:15:00
PedroC88
  • 3,708
  • 7
  • 43
  • 77