Finding if the two DateTime is within a week - c#

I wanted to find if two DateTime are within a week.
One of them is the current system Datetime, which I got it by using:
DateTime CurrentDateTime = new DateTime();
CurrentDateTime = DateTime.Now;
The other DateTime will just be a selected date. Assuming it is stored in a variable called : ExportDate.
So, I can find the difference between them by doing
ExportDate.Subtract(CurrentDateTime)
But I cannot change this value into a int for comparing...
So how should I compare this to DateTime to see if this two dates are greater then 0Days, and less then 7Days.

Subtracting two datetimes gives you a TimeSpan. This class comes with a property called TotalDays.
Gets the value of the current TimeSpan structure expressed in whole and fractional days.
Src: https://msdn.microsoft.com/en-Us/library/system.timespan(v=vs.110).aspx
You can use to count the number of days I guess.
Regards,
Seb

Try to compare the TotalDays of the resulting TimeSpan. Depending on which is later, the result must be between -7 and 7. Use Math.Abs to avoid comparing the value against both bounds:
bool isWithinWeek = Math.Abs(ExportDate - CurrentDate).TotalDays) < 7
If the time of day is irrelevant, compare only the dates:
bool isWithinWeek = Math.Abs(ExportDate.Date - CurrentDate.Date).TotalDays) < 7

You can use
(ExportDate - CurrentDateTime).TotalDays <= 7

You can simply use the DayOfYear property of DateTime to get the day in the year and then check if the difference is less than 7 or not. You can try:
bool isWithinAWeek = Math.Abs(currentDateTime.DayOfYear-exportDate.DayOfYear)>7?false:true;

Related

check if number of days has passed since a date

I am trying to check if a certain number of days has passed since a date and if it has then change the color of the grid row.
So if the date is 12/11/2016 and I want to check if 10 days has passed since that date.
if (dt.Date > dt.Date.AddDays(10))
{
e.Item.Style.Add("background-color", "#C400F9");
break;
}
So adding 10 days to the 12th would be the 22/11/2016 and since today is the 23/11/2016 that means 10 days has passed. But all rows in the grid are changing to the color. Do I need to add another if statement to compare the date + the days passed to today's date?
Did you mean to use today's date? If so, that's DateTime.Now:
if (DateTime.Now > dt.Date.AddDays(10))
At the moment you are comparing dt.Date against same date plus 10 days - as others noted, this will never be met.
Update. As Tim suggests in comments, using DateTime.Today might be more appropriate.
DateTime d1;
DateTime d2;
if((d1 - d2).TotalDays == 10)
{
//some code
}
Your problem is in your comparison. You're trying to figure out if dt is bigger than dt + 10. Obviously it will never be true.
Your comparison will never be true, it's like doing:
If (10 > 10 + 10)...
What you want to do is compare 10 days after the date with today using DateTime.Now
Can you Try this, Below X is date that you want to compare with(it may come form db or hardcoded date)
if (X > dt.Date.AddDays(10))
{
e.Row.Attributes.Add("background-color", "#C400F9");
}
if (DateTime.Now.Date> dt.Date.AddDays(10).Date)
{
e.Item.Style.Add("background-color", "#C400F9");
break;
}
if (DateTime.Now.Date > dt.Date.AddDays(10))
{
}

Difference between two string time and now

I have a string like that 03223311 (hhmmssff). I'm going to compare it with DateTime.Now and see if the difference between these to values is lower than 200 miliseconds.
xdate="03223311";
if(Math.Abs(Convert.ToInt32(xdate) - Convert.ToInt32(DateTime.Now.ToString("hhmmssff")))<200)
I tried to run the line above in a timer with interval of 1 but I can not reach to that condition even if I change xdate to current time... . Do you know how to solve the problem or even a better approach?
string input = "03223311";
var diff = DateTime.Now.TimeOfDay.Subtract(
TimeSpan.ParseExact(input, "hhmmssff", null)
).TotalMinutes; //or any other value like TotalMilliseconds
I would first convert the string into a DateTime so that you can compare apples to apples and utilized the features of the DateTime object. Once you have two DateTime objects, you can subtract them to get a TimeSpan. TimeSpan will have a TotalMilliseconds property that you can compare to your 200 constant.
var xdateValue = DateTime.ParseExact(xdate, "hhmmssff", CultureInfo.InvariantCulture);
var difference = DateTime.Now - xdateValue;
if (difference.TotalMilliseconds < 200) ...
if (((DatetTime.Now - DateTime.ParseExact("03223311 ", "hhmmssff", CultureInfo.InvariantCulture))).Milliseconds > 200)
{
}

Exact c# result of sql datediff

I'm trying to get the number of days (calculated byu datediff) in sql and the number of days in c# (calculated by DateTime.now.Substract) to be the same, but they return different results....
//returns 0
int reso = DateTime.Now.Subtract(expirationDate).Days;
vs
//returns 1
dateDiff(dd,getDate(),ExpirationDate)
In both cases, ExpirationDate is '10/1/2011 00:00:00', and the code and the DB are sitting on the same server. I want the return int to be the same. I suspect I'm missing something stupid... ideas??
dateDiff(dd,getDate(),ExpirationDate) Is doing a days comparison. DateTime.Now.Subtract(expirationDate).Days is doing a date and time
For example
SELECT dateDiff(dd,'10/1/2011 23:59:00' , '10/2/2011') returns one day even when only one minute apart.
If you want the same in C# you need to remove the time component
e.g.
DateTime dt1 = new DateTime(2011,10,1, 23,59,0);
DateTime dt2 = new DateTime(2011,10,2, 0,0,0);
Console.WriteLine((int) dt2.Subtract(dt1.Subtract(dt1.TimeOfDay)));
So in your case it would be something like
DateTime CurrentDate = DateTime.Now;
int reso = CurrentDate.Subtract(CurrentDate.TimeOfDay).Subtract(DateTime.expirationDate).Days;
I haven't tested it but I would not do
DateTime.Now.Subtract(DateTime.Now.Subtract.TimeOfDay)
Because the second call to Now wouldn't be guaranteeing to be the same as first call to Now
In any case Stealth Rabbi's answer seems more elegant anyway since you're looking for a TimeSpan not a DateTime
10/1/2011 is less than 1 day away from DateTime.Now. Since you're getting back a TimeSpan and then applying Days to it, you're getting back a TimeSpan that is < 1 day. So it'll return 0 Days.
Instead, just use the Date component of those DateTimes and it'll correctly report the number of days apart - like this:
DateTime now = DateTime.Now;
DateTime tomorrow = new DateTime(2011, 10, 1);
var val = (tomorrow.Date - now.Date).Days;
This will yield you 1 day.
I'm assuming you want the number of Total days, not the number of days from the largest previous unit. You'd want to use the TotalDays property. Also, you may find it easier to use the minus operator to do a subtraction
DateTime d1 = DateTime.Now;
DateTime d2 = new DateTime(2009, 1, 2);
TimeSpan difference = d1 - d2;
Console.WriteLine(difference.TotalDays); // Outputs (today):1001.46817997424

c# compare datetime for day of month and time

I need to compare two datetime variable only for some criteria :
Day of the month and time .
If the day of the month and the time is equal to another datetime then the expression return true, the comparison must not keep in mind of the month and the year of the two Datetime .
What's the best way to do this comparison ?
Thanks.
Compare the Day and the TimeOfDay properties of the two DateTime values:
bool areEqual = (d1.Day == d2.Day)
&& (Math.Abs((d1.TimeOfDay - d2.TimeOfDay).TotalSeconds) < 1.0);
Adjust for different time zones before comparing if necessary.

How can I increment a date?

I have two dates as duedate as 03/03/2011 and returndate as 03/09/2011. I want to find the fine in double when I subtract duedate from returndate.How can duedate be incremented?
Following code might help you:
var dueDate = new DateTime(2011, 3, 3);
//if you want to increment six days
var dueDatePlus6Days = dueDate.AddDays(6);
//if you want to increment six months
var dueDatePlus6Months = dueDate.AddMonths(6);
var daysDiff1 = (dueDatePlus6Days - dueDate).TotalDays; //gives 6
var daysDiff2 = (dueDatePlus6Months - dueDate).TotalDays; //gives 184
The logical solution would seem to be the AddDays method, as in the other answers.
However, I try (in general) never to use floating point (i.e. a double) when working with monetary or date values.
DateTime contains a time component and AddDays takes a double as argument (fractional part becomes time), so I tend to avoid use of that method.
Instead, I use
dueDatePlusOne = dueDate.AddTicks(TimeSpan.TicksPerDay);
This should result in slightly faster execution too. Not that it still matters much on today's hardware, but I started out coding for microprocessors with a <1 MHz clock speed and old PDP-8's and -11's and stuff like that back in the 1970's, and some habits never die ;)
Assuming returnDate, dueDate are DateTime objects:
double extraDays = (returnDate - dueDate).TotalDays;
May this can help you
DateTime dt_duedate = DateTime.Now;
DateTime dt_returndate = DateTime.Now.AddDays(2);
System.TimeSpan diffResult = dt_returndate.Subtract(dt_duedate);

Categories