I want to detect the 1st of april of every year in Csharp. I am worki开发者_如何学Pythonng on an ASP.Net / C# project where there is expiration of registration is set on 1st April. So what my approach is to check current system date on page load and if matches the date 1st april, the some event will fire.
if ((DateTime.Now.Month == 4) && (DateTime.Now.Day == 1))
{
// do something
}
If you want to check whether or not the date is before 1st April then you can do something like this. (Presumably you're talking about this year.)
if (DateTime.Now < new DateTime(2011, 4, 1))
{
// not 1st april yet
}
else
{
// it's 1st april or later
}
if (DateTime.Now.Month == 4 && DateTime.Now.Day == 1)
DoYourActionForApril1st();
Something like this maybe:
if (DateTime.Today.Month == 4 && DateTime.Today.Day == 1) {
// it's April 1st!
}
However, if you have something that is valid before April 1st and not after, you're better off checking if the current date is before or after april 1st:
var April1st = new DateTime(DateTime.Now.Year, 4, 1);
if (DateTime.Now >= April1st) {
// Cannot register anymore this year
}
else {
// OK, it's before April 1st.
}
精彩评论