Difference between two dates # - c#

I have to calculate number of days difference between today and SubmittedDate, but if I the SubmittedDate = today
my Result = 0,430090... Instead of 1
here is my code:
DaysDiff = (today.Subtract(DataUtilities.GetSafeDateTime(financialStatement[SharePoint_Assessment_Fields.SUBMITTEDDATE_FIELD]))).TotalDays,
could you please help me ?

The TotalDays property is a double. It also takes the hours and minutes in account, so that might cause the subtraction of two days get fractions too.
If you want to round that, you could use Math.Round, Math.Ceiling or Math.Floor depending on your needs. Taking your expected outcome, I guess you need to use Ceiling:
double ceiledDays = Math.Ceiling(ts.TotalDays);
Or you could get the Date part of the two dates and calculate with that.

Related

Difference in days between two dates in C# - returns integer

This code
(this.ApprovedDate - this.ReceivedDate).TotalDays
gives me a double typed value.
How do I get the integer one rounded up? Assuming that this.ApprovedDate and this.ReceivedDate are both DateTime type.
You need to use the Math library and cast to int.
Example:
var a = new TimeSpan(5, 14, 0, 0); // 5 days, 14 hours
var x = a.Days; // Does not round up. = 5
var y = (int) Math.Round(a.TotalDays); // Rounds up. = 6
TotalDays gets the value of the current TimeSpan structure expressed in whole and fractional days. Instead you should be using the integer Days property which gets the days component of the time interval represented by the current TimeSpan structure, see the documentation.
Please note that using Days gives the whole number of days between two dates and will ignore fractions. Depending on your requirements you may want to round TotalDays to zero digits instead and cast it to an integer as proposed by MutantNinjaCodeMonkey.

Difference between two times follow up (Convert to decimal)

I asked a question like this earlier and got a great answer but I made a mistake and wanted the output to be a decimal rather than a time. So here's the question.
I have two textboxes that allow a user to enter a start time and an end time in this format (h:mm). I want it to return the difference in a label. For example, if a user enters 1:35 in the first textbox and 3:30 in the second textbox and press the 'Calculate' button, it will return the decimal 1.92.
Any ideas or resources for this? I only want to calculate decimal difference of the time entered, date and seconds doesn't matter at all. Below is the code for getting an output in the format of (h:mm).
TimeSpan ts1 = TimeSpan.Parse(textBox1.Text); //"1:35"
TimeSpan ts2 = TimeSpan.Parse(textBox2.Text); //"3:30"
label.Text = (ts2 - ts1).ToString(); //"1:55:00"
It sounds like you want the total number of hours, in that 1.92 hours is 115 minutes (ish).
In that case you want:
double hours = (ts2 - ts1).TotalHours;
... you can then format that how you wish (e.g. to 2 decimal places).
For example:
TimeSpan ts1 = TimeSpan.Parse("1:35");
TimeSpan ts2 = TimeSpan.Parse("3:30");
double hours = (ts2 - ts1).TotalHours;
Console.WriteLine(hours.ToString("f2")); // Prints 1.92
Of course I'd personally use Noda Time and parse the strings as LocalTime values instead of TimeSpan values, given that that's what they're meant to be (times of day), but that's a minor quibble ;)
(ts2 - ts1).TotalHours.ToString();

Get Hours and Minutes from Datetime

Out Time :
2013-03-08 15:00:00.000
In Time :
2013-03-08 11:21:03.290
I need to get Hours and Minutes separately for same date from above, when (Out Time - In Time).
How can I do that ?
I think you probably just want:
TimeSpan difference = outTime - inTime;
int hours = (int) difference.TotalHours;
int minutes = difference.Minutes;
Note that Minutes will give you "just the minutes (never more than 59)" whereas TotalHours (truncated towards zero) will give you "the total number of hours" which might be more than 23 if the times are more than a day apart.
You should also consider what you want to do if the values are negative - either consider it, or explicitly rule it out by validating against it.
The Subtract method on the DateTime class will allow you subtract that date from the other date.
It will give you a TimeSpan which will be the difference.
I'll leave it to you to work out the actual code.
http://msdn.microsoft.com/en-GB/library/8ysw4sby.aspx
You can use Hours property and Minutes
link : http://msdn.microsoft.com/en-us/library/system.datetime.hour.aspx

timeoffset in whole minutes in c#

I have a method that calculates the number of minutes between two times. Something like this:
DateTime UserLocalTime = DateTime.UtcNow.ConvertToUserTime(UserTimezoneID);
double TheOffset = (UserLocalTime - DateTime.UtcNow).TotalMinutes;
return TheOffset.ToString();
The problem is that it's returning something like 119.83723 because it's calculating minutes AND seconds and such.
How can I fix this? I'm afraid sometimes it'll also return 120.11233 so rounding might not totally help either.
Thanks for your suggestions.
Update1: (int)(Math.Round((UserLocalTime - DateTime.UtcNow).TotalMinutes)); returns 119.
Update2: I tried this, it looks like it works but will fail when the offset is weird (sometimes it might be 5:30 and the answer should be 330 minutes)
int TheOffset = (int)(Math.Round((UserLocalTime - DateTime.UtcNow).TotalHours));
TheOffset = TheOffset * 60;
Do you want to get only the minutes and nothing else? Just use Minutes instead of TotalMinutes.
Do you want to get the total number of minutes, but not any measure beneath it? Use (int)TotalMinutes. It'll round down.
I'm afraid sometimes it'll also return 120.11233 so rounding might not totally help either.
Can you clarify? Rounding 120.11233 will result in 120... I think that's what you want, right?
You have can use:
Math.Round() to get the nearest integer
Math.Floor() to get the largest integer less than or equal to the specified number.
Math.Ceiling() to get the smallest integral value greater than or equal to the specified number.
119,895 -> Floor= 119, Ceiling = 120, Round = 120
119,175 -> Floor= 119, Ceiling = 120, Round = 119
Apply the desired function to your TheOffset.
If you need Ceiling or Floor, you should probably invert the function used if negative or positive offset.
So, you decide what you want to get.

Disparity between date/time calculations in C# versus Delphi

Delphi:
SecondsBetween(StrToDateTime('16/02/2009 11:25:34 p.m.'), StrToDateTime('1/01/2005 12:00:00 a.m.'));
130289133
C#:
TimeSpan span = DateTime.Parse("16/02/2009 11:25:34 p.m.").Subtract(DateTime.Parse("1/01/2005 12:00:00 a.m."));
130289134
It's not consistent either. Some dates will add up the same, ie..
TimeSpan span = DateTime.Parse("16/11/2011 11:25:43 p.m.").Subtract(DateTime.Parse("1/01/2005 12:00:00 a.m."));
SecondsBetween(StrToDateTime('16/11/2011 11:25:43 p.m.'), StrToDateTime('1/01/2005 12:00:00 a.m.'));
both give
216905143
The total amount of seconds is actually being used to encode data, and I'm trying to port the application to C#, so even one second completely throws everything off.
Can anybody explain the disparity? And is there a way to get c# to match delphi?
Edit: In response to suggestions that it might be leap second related: Both date ranges contain the same amount of leap seconds (2), so you would expect a mismatch for both. But instead we're seeing inconsistency
16/02/2009 - 1/01/2005 = Delphi and C# calculate a different total seconds
16/11/2011 - 1/01/2005 = They calculate the same total seconds
The issue it seems related to this QC 59310, the bug was fixed in Delphi XE.
One will likely deal with Leap Seconds. However, .NET does not as far as I'm aware.
You don't mention how you convert the c# TimeSpan into a number. The TotalSeconds property is a floating point value - perhaps it's a rounding problem in the double to int conversion?

Categories