0

I'm having this weird problem when it comes to dates. I'm trying to get a value from mongodb using date. I know something happened that's why even if they have the same date, nothing is being returned. Here's the process:

I'm pulling the data from the database using an Id, now the record I'm getting has a date. This date is being displayed on my view. Here's how I do it.

Action:

public JsonResult GetDateSentList(string id)
{
    var data = ApiHelper<List<SomeModel>>.Get("SomeController/SomeAction?id=" + id); //I created a helper for requests
    var distinctData = data.Select(a => a.DateOfEvent).Distinct().ToList();
    foreach (var item in distinctData)
    {
        var dateData = new SomeModel();
        dateData = data.FirstOrDefault(a => a.DateOfEvent == distinctData[i]);
        dateData.DateOfEventString = dateData.DateOfEvent.ToString(@"MM-dd-yyyy hh:mm", new CultureInfo("en-US"));
        dateData.DateOfEventExactString = dateData.DateOfEvent.ToString(@"MM-dd-yyyy hh:mm:ss tt", new CultureInfo("en-US"));

    }
    return Json(listDistinctData);
}

As you can notice this is being accessed by an ajax call and the values here (date strings) are being displayed using js.

Here's the value I'm getting at this point: 6/30/2014 7:01:17 AM (this is now a string in html value)

Now, at a button press, I'm passing this same date string on an action and this action performs a request to a WebApi.

On the WebApi, the string is being converted to dateTime (request.DateSent) and at this point I'm trying to get the same record that I got on the first part using the date that I got on the record that I want to get.

x= someService.All().FirstOrDefault(c => c.DateOfEvent == request.DateSent);

I checked the record that I wanted to get, it has a DateTime value 6/30/2014 7:01:17 AM and the DateTime that I'm using to get the record that I wanted has the same DateTime value 6/30/2014 7:01:17 AM. Sadly, I keep on getting a null value. Something happened here that caused them to be not equal, but I can't trace it at all. Any ideas? Thanks!

Luke Villanueva
  • 2,030
  • 8
  • 44
  • 94
  • 3
    Maybe the milliseconds (which are not shown) are different? Also, possibly some timezone issues. – Davio Jul 01 '14 at 17:41
  • Is that so, so I really need to have the exact same values down to every detail. Hmm, how can I compare them with just taking note of this format (mm-dd-yy hh:mm:ss tt)? – Luke Villanueva Jul 01 '14 at 18:30
  • You're using `foreach` to get each individual `item` yet you never use the `item` variable inside the loop! Shouldn't the second line inside the foreach read `dateData = data.FirstOrDefault(a => a.DateOfEvent == item);`? If not, what is the value of the variable `i` inside the `foreach`? – Chris Dunaway Jul 01 '14 at 21:34

1 Answers1

1

You have to think about it some more. Is 1 millisecond past midnight the same as 1 millisecond before midnight? If so, you should calculate the difference and see whether it's less than some interval you choose. If not, you should only compare the fields you're interested in (year, day, month, etc).

You could build a static "truncate" function which chops off everything but seconds and higher: dt = dt.AddTicks(-dt.Ticks % 10000000);

Found here: Ignore milliseconds when comparing two datetimes

And use the truncated dates for comparison.

Be wary of timezones, but that's probably not an issue here.

Community
  • 1
  • 1
Davio
  • 4,609
  • 2
  • 31
  • 58