how to compare month-year with DateParse
Asked Answered
A

6

11

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...

Antarctica answered 24/10, 2011 at 9:31 Comment(4)
What's wrong with what you have now?Pyrognostics
Will Parse always work for you ? parse it once and then play around with the values from the parsed instance, would be simplerTantalous
for example, I have to check if 10-2011 (now) is between 01-2011 and 04-2012...Antarctica
Why are there three dates? Are there any more dates than o.MyDate and DateTime.Now?Licastro
A
15

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 <.
}
Accrete answered 24/10, 2011 at 9:38 Comment(5)
I've update my target. It won't work with this code. Sorry, wasnt clear :) Hope now you can understand what I'm looking for...Antarctica
I know how to do this using single month or year. But is not what I asked :) Is if I can check directly the pari month-year...Antarctica
@markzzz: My answer checks exactly what you asked. It checks if a certain date is in a range of dates. Month/Year is a date, too, it is equivalent to 01/Month/Year. Please try to understand the code again. I updated my answer to make it more explicit.Accrete
yes, but checking first year, than month. not directly year-month. That's what I asked...but ok if I can't do it I'll use your own code :)Antarctica
Thankyou, very simple but very hard at the same time... And more when you have some days coding continously...Norrie
T
8
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;
Trafficator answered 24/10, 2011 at 9:36 Comment(0)
U
4
DateTime dateCheck = DateTime.Parse(o.MyDate);
bool result = ((Now.Month - dateCheck.Month) + 12 * (Now.Year - dateCheck.Year)) > 0
Upswell answered 24/10, 2011 at 9:40 Comment(0)
D
1
var date1 = new DateTime(year1, month1, 1);
var date2 = new DateTime(year2, month2, 1);

if(date1 < date2)...
Draw answered 17/11, 2020 at 20:13 Comment(0)
S
0
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");
}
Slew answered 31/7 at 10:16 Comment(1)
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Mccalla
N
-1
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

Nymphomania answered 9/6, 2016 at 4:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.