-1

I am trying to compare DateTimeOffset types. I am checking that the start date is not greater than the end date, but even when the start date is not greater than the end date it fails.

I debugged and hovered over this bit of code that is throwing the exception. The start value is 8/29/2013 and the end date is 9/1/2014.

Example Code:>

public IEnumerable<RecruitingPerformance> GetDailyRecruitingPerformance(DateTimeOffset start, DateTimeOffset end, int? userId, int? projectId)
{
    if (end > DateTimeOffset.UtcNow)
    {
        throw new ArgumentException("End date must be today or before.", "end");
    }
    if (start > end)
    {
        throw new ArgumentException("Date must be greater than or equal to start", "end");
    }

Picture of trouble spot:> enter image description here

Here is picture of the two objects for the non believers: enter image description here

allencoded
  • 7,015
  • 17
  • 72
  • 126
  • Did you check that the start and end date belong to the same time zone? This might be the issue... – LazyOfT Jul 22 '14 at 19:22
  • @Brizio according to OP the start and end values are more than a year apart. – vcsjones Jul 22 '14 at 19:23
  • yeah they are plus on top of that they are in the same time zone -5:00 – allencoded Jul 22 '14 at 19:24
  • 4
    Please show a short but *complete* program demonstrating the problem. I don't believe that DateTimeOffset is that broken... – Jon Skeet Jul 22 '14 at 19:28
  • Also, you might want to try to rebuild your project, as it might also be that you are debugging a different version. – LazyOfT Jul 22 '14 at 19:30
  • 2
    I still think you are debugging an old version, as according to your code it should throw on the first if clause, not on the second. – LazyOfT Jul 22 '14 at 19:32
  • 2
    @Brizio Perhaps he's posting this from the future? Have you considered that? I mean, Jon Skeet once posted an answer before the question was asked - http://meta.stackexchange.com/a/9135 - You never know. – Lasse V. Karlsen Jul 22 '14 at 19:48

1 Answers1

0

Why not use the DateTimeOffset.Compare() method?

DateTimeOffset start = new DateTimeOffset(2013, 8, 29, 12, 0, 0, new TimeSpan(-5, 0, 0));
DateTimeOffset end = new DateTimeOffset(2014, 9, 1, 12, 0, 0, new TimeSpan(-5, 0, 0));

// if compare brings back 1, start is greater than end
if(DateTimeOffset.Compare(start, end) > 0)
    throw new ArgumentException("Date must be greater than or equal to start", "end");
Cory
  • 1,794
  • 12
  • 21