Zenoheld
Zenoheld

Reputation: 37

Need specific dates in C#

I have an application in WindowsForms where I have to add exceptions if the day is 25th December and 1st January so is something like this if(DateTime.Now== 25thJanuary) I don't care about the year just the day, so is it possible for C# to get these days?

Upvotes: 1

Views: 96

Answers (4)

Evgeni Velikov
Evgeni Velikov

Reputation: 382

Well you have two choose to find do is 25th December or 1st January. First is use DateTime.Now this is return not only day, is return and hours and minutes and seconds (for seconds I'm not sure).

In my example I use DateTime.Today who return Year Month and Day. and return default hour minutes and seconds.

if ((DateTime.Today.Month == 12 && DateTime.Today.Day == 25) || (DateTime.Today.Day == 1 && DateTime.Today.Month == 1))
{
     // do some stuff
}

I'm not sure but for performance maybe Today is better from Now because is not check what time is now. Today is check only what date is now (If we can talking here for performance).

Upvotes: 0

Santosh Kokatnur
Santosh Kokatnur

Reputation: 364

using System.Diagonastics;
using System.Threading.Tasks; 

DateTime now = DateTime.now;
if(now.Day == 25 && now.Month == 12)
{
  ....................
  //Put your code here
  ....................
}

Upvotes: 0

Mark
Mark

Reputation: 2061

DateTime now = DateTime.Now;
if (now.Day == 25 && now.Month == 12)

you could check the day and the month of today like this

Upvotes: 3

Martin
Martin

Reputation: 664

Easy. For checking 25th December you can do,

if(DateTime.Now.Month == 12 && DateTime.Now.Day == 25)

Upvotes: 2

Related Questions