如何比较DateTime C#月和周

如何比较DateTime C#月和周,c#,datetime,c#-2.0,C#,Datetime,C# 2.0,我需要用C来比较一个日期# 如果日期小于12个月,我需要设置一个布尔值 我的代码是 String d = "26/06/10"; DateTime dt = DateTime.ParseExact(d, "dd/MM/yy", null); if ((dt > DateTime.Now.AddMonths(-12) ) ) { Console.WriteLine("It is less than 12 months"); }

我需要用C来比较一个日期# 如果日期小于12个月,我需要设置一个布尔值

我的代码是

    String d  = "26/06/10";
    DateTime dt = DateTime.ParseExact(d, "dd/MM/yy", null);
    if ((dt > DateTime.Now.AddMonths(-12)  ) )
    {
        Console.WriteLine("It is less than 12 months");
    }
    else
    {
        Console.WriteLine("It is more than 12 months");
    }
是比较c#中日期的最佳方法。 同样,我需要比较日期是否少于两周

谢谢你的帮助

谢谢

两周的sup:

if (dt1.Subtract(dt2).Days > 14)
{
    ...
}  
12个月(一年)(考虑到每月的哪一天不重要):

你可以

DateTime date2 = DateTime.Now.AddMonths(-12);
  //Or if you want to neglect the time part you could do
DateTime date2 = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,0,0,0).AddMonths(-12);
String d = "26/06/10"; 
DateTime date1 = DateTime.ParseExact(d, "dd/MM/yy", null);
int result = DateTime.Compare(date1, date2);
string res;

if (result < 0)
   Console.WriteLine("It is less than 12 months"); 
else if (result == 0)
   res = "is the equal";         
else
    Console.WriteLine("It is more than 12 months"); 
DateTime date2=DateTime.Now.AddMonths(-12);
//或者如果你想忽略你可以做的时间部分
DateTime date2=新的日期时间(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,0,0,0).AddMonths(-12);
字符串d=“26/06/10”;
DateTime date1=DateTime.ParseExact(d,“dd/MM/yy”,null);
int result=DateTime.Compare(date1,date2);
字符串res;
如果(结果<0)
Console.WriteLine(“不足12个月”);
否则如果(结果==0)
res=“是相等的”;
其他的
Console.WriteLine(“超过12个月”);

代码片段的问题是,即使日期相等,它也会输出“超过12个月”。

您可以使用
TimeSpan
获取两个
DateTime
值之间的差值

String d = "26/06/10";
DateTime dt = DateTime.ParseExact(d, "dd/MM/yy", null);
DateTime dt2 = DateTime.Now.AddMonths(-12);

TimeSpan ts = dt - dt2;

您可以使用
ts.Days
来比较以获得更清晰的理解:您不希望比较两个日期(或日期时间),而是比较两个时间跨度。即现在与您提供的日期之间的时间差,以及12个月的时间跨度

String d = "26/06/10";
DateTime dt = DateTime.ParseExact(d, "dd/MM/yy", CultureInfo.InvariantCulture);
TimeSpan deltaTimeSpan = dt - DateTime.Now;      // get the time difference between now and the time given
TimeSpan twelveMonths = new TimeSpan(365,0,0,0); // get a time span of 12 months

// round the amount of days down and always supply a positive number of days
int deltaTime = Convert.ToInt32(Math.Abs(Math.Floor(deltaTimeSpan.TotalDays)));

if (twelveMonths.TotalDays > deltaTime)
{
    Console.WriteLine(string.Format("It is less than 12 months ({0} days).", deltaTime));
}
else if (twelveMonths.TotalDays < deltaTime)
{
    Console.WriteLine(string.Format("It is more than 12 months ({0} days).", deltaTime));
}
else
{
    Console.WriteLine(string.Format("The difference in time is exactly 12 months. ({0} days).", deltaTime);
}
String d=“26/06/10”;
DateTime dt=DateTime.ParseExact(d,“dd/MM/yy”,CultureInfo.InvariantCulture);
TimeSpan deltaTimeSpan=dt-DateTime.Now;//获取现在与给定时间之间的时差
TimeSpan十二个月=新的TimeSpan(365,0,0,0);//获得12个月的时间跨度
//将天数向下四舍五入,并始终提供正天数
int deltaTime=Convert.ToInt32(Math.Abs(Math.Floor(deltaTimeSpan.TotalDays));
if(十二个月.TotalDays>deltaTime)
{
WriteLine(string.Format(“它少于12个月({0}天)。”,deltaTime));
}
else if(十二个月。总天数
请注意,此示例当然没有考虑闰年。代码确实考虑了要与过去或未来的年份进行比较的年份(通过将时间跨度转换为正值并与该值进行比较)


调整上述代码,使其在两周内或任何其他时间跨度内保持相同,应该足够简单。只需更改我命名为“Twelvenmonds”的时间跨度即可.

如果dt1=2011年12月31日,dt2=2012年1月1日,你的第二个例子难道不是真的吗?这是OP真正想要的吗?嗯,你说得对。我没有考虑这个问题。很抱歉,这个愚蠢的回答我认为你现在做的是最好的方式。即使你使用TimeSpan
TotalDays
也会合适,而不是
Days
,你可以n还使用TimeSpan.Duration来确保时间跨度为非负。
String d = "26/06/10";
DateTime dt = DateTime.ParseExact(d, "dd/MM/yy", CultureInfo.InvariantCulture);
TimeSpan deltaTimeSpan = dt - DateTime.Now;      // get the time difference between now and the time given
TimeSpan twelveMonths = new TimeSpan(365,0,0,0); // get a time span of 12 months

// round the amount of days down and always supply a positive number of days
int deltaTime = Convert.ToInt32(Math.Abs(Math.Floor(deltaTimeSpan.TotalDays)));

if (twelveMonths.TotalDays > deltaTime)
{
    Console.WriteLine(string.Format("It is less than 12 months ({0} days).", deltaTime));
}
else if (twelveMonths.TotalDays < deltaTime)
{
    Console.WriteLine(string.Format("It is more than 12 months ({0} days).", deltaTime));
}
else
{
    Console.WriteLine(string.Format("The difference in time is exactly 12 months. ({0} days).", deltaTime);
}
DateTime date1 = DateTime.Now.AddMonths(-12)
if(DateTime.Compare(dt, date1 )
{
//provided date is within 12 months
}
else
{
//provided date is after 12 months
}