在 C# 中需要特定日期

Need specific dates in C#

我在 WindowsForms 中有一个应用程序,如果这一天是 12 月 25 日和 1 月 1 日,我必须添加例外情况,所以是这样的 if(DateTime.Now== 25thJanuary)我不在乎年份只在乎日期,那么C#有没有可能得到这些天数?

简单。要检查 12 月 25 日,您可以这样做,

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

你可以像这样查看今天的日期和月份

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

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

好吧,您有两个选择:12 月 25 日或 1 月 1 日。首先是使用 DateTime.Now 这 return 不仅是天,还有 return 和小时、分钟和秒(我不确定秒)。

在我的示例中,我使用 DateTime.Today who return 年月日。和 return 默认时分秒。

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

我不确定,但对于性能来说,也许今天比现在更好,因为它不会检查现在几点。今天只是检查现在是什么日期(如果我们可以在这里讨论性能)。