如何使用 DateParse 比较月年

发布于 2024-12-11 07:40:11 字数 397 浏览 0 评论 0原文

我必须检查日期(月年)是否小于实际日期。

我知道如何仅对单个月份或年份执行此操作,例如

DateTime.Parse(o.MyDate).Month <= DateTime.Now.Month

DateTime.Parse(o.MyDate).Year <= DateTime.Now.Year

,但如何直接检查月年是否小于 now.month-now.year ?

编辑

例如,我要做的是检查 10-2011 (DateTime.Now.Month-DateTime.Now.Year) 是否在 01-2011 和 04-2012 之间...

I have to check if a date (month-year) is minus than actual date.

I know how to do it only with single month or year, like

DateTime.Parse(o.MyDate).Month <= DateTime.Now.Month

or

DateTime.Parse(o.MyDate).Year <= DateTime.Now.Year

but how can I check directly if month-year is minus than now.month-now.year?

EDIT

What I have to do is, for example, to check if 10-2011 (DateTime.Now.Month-DateTime.Now.Year) is between 01-2011 and 04-2012...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

深海夜未眠 2024-12-18 07:40:11

如果年份相同,则比较月份,如果年份不同,则您的年份必须小于现在:

var yourDate = ...;
if((yourDate.Year == DateTime.Now.Year && yourDate.Month < DateTime.Now.Month)
   || yourDate.Year < DateTime.Now.Year)
{
    // yourDate is smaller than todays month.
}

更新:

要检查 yourDate 是否在一定的时间范围,使用这个:

var yourDate = ...;
var lowerBoundYear = 2011;
var lowerBoundMonth = 1;
var upperBoundYear = 2012;
var upperBoundMonth = 4;

if(((yourDate.Year == lowerBoundYear && yourDate.Month >= lowerBoundMonth) || 
    yourDate.Year > lowerBoundYear
   ) &&
   ((yourDate.Year == upperBoundYear && yourDate.Month <= upperBoundMonth) ||
    yourDate.Year < lowerBoundYear
   ))
{
    // yourDate is in the time range 01/01/2011 - 30/04/2012
    // if you want yourDate to be in the range 01/02/2011 - 30/04/2012, i.e. 
    // exclusive lower bound, change the >= to >.
    // if you want yourDate to be in the range 01/01/2011 - 31/03/2012, i.e.
    // exclusive upper bound, change the <= to <.
}

If the years are the same, compare the months, if the years are not the same, your year must be smaller than now:

var yourDate = ...;
if((yourDate.Year == DateTime.Now.Year && yourDate.Month < DateTime.Now.Month)
   || yourDate.Year < DateTime.Now.Year)
{
    // yourDate is smaller than todays month.
}

UPDATE:

To check if yourDate is in a certain time range, use this:

var yourDate = ...;
var lowerBoundYear = 2011;
var lowerBoundMonth = 1;
var upperBoundYear = 2012;
var upperBoundMonth = 4;

if(((yourDate.Year == lowerBoundYear && yourDate.Month >= lowerBoundMonth) || 
    yourDate.Year > lowerBoundYear
   ) &&
   ((yourDate.Year == upperBoundYear && yourDate.Month <= upperBoundMonth) ||
    yourDate.Year < lowerBoundYear
   ))
{
    // yourDate is in the time range 01/01/2011 - 30/04/2012
    // if you want yourDate to be in the range 01/02/2011 - 30/04/2012, i.e. 
    // exclusive lower bound, change the >= to >.
    // if you want yourDate to be in the range 01/01/2011 - 31/03/2012, i.e.
    // exclusive upper bound, change the <= to <.
}
多情癖 2024-12-18 07:40:11
var date = DateTime.Parse(o.MyDate);
var year = date.Year;

// We don't even want to know what could happen at 31 Dec 23.59.59 :-)
var currentTime = DateTime.Now;
var currentYear = currentTime.Year;

bool result = year < currentYear || 
                 (year == currentYear && 
                     date.Month <= currentTime.Month)

第二个选择:

var date = DateTime.Parse(o.MyDate).Date; // We round to the day
date = date.AddDays(-date.Day); // and we remove the day

var currentDate = DateTime.Now.Date;
currentDate = currentDate.AddDays(-currentDate.Day);

bool result = date <= currentDate;

第三个选择(也许更“老派”)

var date = DateTime.Parse(o.MyDate);
var currentTime = DateTime.Now;

// Each year can be subdivided in 12 parts (the months)
bool result = date.Year * 12 + date.Month <= currentTime.Year * 12 + currentTime.Month;
var date = DateTime.Parse(o.MyDate);
var year = date.Year;

// We don't even want to know what could happen at 31 Dec 23.59.59 :-)
var currentTime = DateTime.Now;
var currentYear = currentTime.Year;

bool result = year < currentYear || 
                 (year == currentYear && 
                     date.Month <= currentTime.Month)

Second option:

var date = DateTime.Parse(o.MyDate).Date; // We round to the day
date = date.AddDays(-date.Day); // and we remove the day

var currentDate = DateTime.Now.Date;
currentDate = currentDate.AddDays(-currentDate.Day);

bool result = date <= currentDate;

Third option (more "old school" perhaps)

var date = DateTime.Parse(o.MyDate);
var currentTime = DateTime.Now;

// Each year can be subdivided in 12 parts (the months)
bool result = date.Year * 12 + date.Month <= currentTime.Year * 12 + currentTime.Month;
滥情稳全场 2024-12-18 07:40:11
DateTime dateCheck = DateTime.Parse(o.MyDate);
bool result = ((Now.Month - dateCheck.Month) + 12 * (Now.Year - dateCheck.Year)) > 0
DateTime dateCheck = DateTime.Parse(o.MyDate);
bool result = ((Now.Month - dateCheck.Month) + 12 * (Now.Year - dateCheck.Year)) > 0
甚是思念 2024-12-18 07:40:11
var date1 = new DateTime(year1, month1, 1);
var date2 = new DateTime(year2, month2, 1);

if(date1 < date2)...
var date1 = new DateTime(year1, month1, 1);
var date2 = new DateTime(year2, month2, 1);

if(date1 < date2)...
高速公鹿 2024-12-18 07:40:11
DateTime date1 = new DateTime(2011, 1, 1, 0, 0, 0);
DateTime date2 = new DateTime(2011, 2, 1, 0, 0, 0);

if (DateTime.Parse(date1.ToString("MM-yyyy")) <= DateTime.Parse(date2.ToString("MM-yyyy")))
{
    // Some code
    Console.WriteLine("date1 is less than or equal to date2");
}
else
{
    // Some code
    Console.WriteLine("date1 is greater than date2");
}
DateTime date1 = new DateTime(2011, 1, 1, 0, 0, 0);
DateTime date2 = new DateTime(2011, 2, 1, 0, 0, 0);

if (DateTime.Parse(date1.ToString("MM-yyyy")) <= DateTime.Parse(date2.ToString("MM-yyyy")))
{
    // Some code
    Console.WriteLine("date1 is less than or equal to date2");
}
else
{
    // Some code
    Console.WriteLine("date1 is greater than date2");
}
慢慢从新开始 2024-12-18 07:40:11
Date date1 = new Date(2011, 1, 1, 0, 0, 0);
Date date2 = new Date(2011, 2, 1, 0, 0, 0);

int result = DateCompare(date1, date2);

如果结果是< 0 则日期 1 <日期2
如果结果为 0,则 date1 == date2
如果结果是> 0 日期1>日期2

Date date1 = new Date(2011, 1, 1, 0, 0, 0);
Date date2 = new Date(2011, 2, 1, 0, 0, 0);

int result = DateCompare(date1, date2);

if the result is < 0 then date1 < date2
if the result is 0 then date1 == date2
if the result is > 0 the date1 > date2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文