Reputation: 780
is it possible to have something that can compare two dates in this format.. example today "Sep 30, 2013" and one week ago "Sep 22, 2013" if its within this range say "good" if older then say "not good" how can I make this in C# or vb.net
Upvotes: 2
Views: 1008
Reputation: 780
thanks to all users who helped me here.. this is the final code that I am using..
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fileDate As Date = Convert.ToDateTime("Sep 25, 2013")
Dim rightNow As Date = Convert.ToDateTime(Date.Now.ToString("MMM dd, yyyy"))
Dim lastWeek = rightNow.AddDays(-7)
If rightNow >= fileDate And lastWeek <= fileDate Then
Debug.Print("its new")
Else
Debug.Print("too old")
End If
End Sub
Upvotes: 0
Reputation: 236328
You can use this generic Range class for checking some comparable value (like DateTime) falls into range:
public class Range<T>
where T : IComparable
{
public Range(T from, T to)
{
if (from.CompareTo(to) > 0)
throw new ArgumentException("From should not be greater than To");
From = from;
To = to;
}
public T From { get; private set; }
public T To { get; private set; }
public bool Contains(T value)
{
return value.CompareTo(From) >= 0 && value.CompareTo(To) <= 0;
}
// other methods like Intersects etc
}
Usage:
var today = DateTime.Today;
var range = new Range<DateTime>(today.AddDays(-7), today);
DateTime date = new DateTime(2013, 9, 25);
if (range.Contains(date))
// say good
Upvotes: 0
Reputation: 2221
if(date1 >= Convert.ToDateTime("Sep 22, 2013") && date1 <= Convert.ToDateTime("Sep 30, 2013"))
{
good
}
else
{
bad
}
Upvotes: 0
Reputation: 23117
If difference between dates is smaller than 7 days, it will print "good" otherwise "Not good"
var ok = (firstDate-secondDate).TotalDays < 7? "good": "Not good";
Console.WriteLine(ok);
TotalDays
is double
so you can check if differnce is in full days, use Days
if you want completed days difference as int
Read more about TimeSpan and it's properties.
Upvotes: 4