views:

146

answers:

3

I have 2 integer fields that represent dates in the YYYYMMDD format. What is the best way to subtract 2 of these fields to get the correct # of days between them?

For instance, if I take the difference between 20100511 and 20100428 I would like the result to be 13 and not 83. I know I need to convert the integer fields into date formats but everything I have tried either throws an exception or doesn't work correctly.

What am I missing? Answers in vb.net please

+2  A: 
    Dim dt1 As Integer = 20100510
    Dim dt2 As Integer = 20100520
    Dim date1 As DateTime = DateTime.ParseExact(dt1.ToString(), "yyyyMMdd", CultureInfo.InvariantCulture)
    Dim date2 As DateTime = DateTime.ParseExact(dt2.ToString(), "yyyyMMdd", CultureInfo.InvariantCulture)
    Dim nDays As Integer = date1.Subtract(date2).Days
dcp
This doesn't mention the required parsing at all.
Noldorin
Fixed. Just a note though, before you downvote somebody and tell another person your answer is "otherwise right", you might want to actually bother confirming it yourself. He didn't have the CultureInfo parameter (he has since fixed it) which is required for ParseExact, so you were wrong as well.
dcp
Yes, that's why I said untested. :) You made the same mistake I did with the custom string format (YYYYmmdd should be yyyyMMdd). We were all wrong.
Nelson
I ran the code, but it didn't give me an error on the parse so I didn't realize it at first glance. I wasn't faulting you here, I was pointing out that Noldorin downvoted my answer without confirming yours (and even telling you it was right), which was just a little frustrating from my viewpoint. But no worries, nice job on your answer :).
dcp
Understandable... My original answer was only meant to point in the right direction, but with comments I was getting I thought it would be best to put a correct answer. For something this simple it's probably best to test it before posting... :S
Nelson
Sure, no worries at all :). Nelson had the better answer here because I did indeed miss the parsing aspect of the question, so he surely deserves the bulk of votes.
dcp
+8  A: 

It should be something like this (untested!)

Dim date1 As DateTime = DateTime.ParseExact(yourdate1.ToString(), "yyyyMMdd", CultureInfo.InvariantCulture)
Dim date2 As DateTime = DateTime.ParseExact(yourdate2.ToString(), "yyyyMMdd", CultureInfo.InvariantCulture)
Dim days As Integer = date1.Subtract(date2).Days
Nelson
I think you need `yourdate.ToString()` for the asker's scenario, but otherwise right. :)
Noldorin
Good point. I had edited to say yourdate had to be a string, but it's not what the poster had.
Nelson
Your format strings are wrong. `mm` is minutes and `YYYY` doesn't exist.
Matthew Whited
That's what I get for not testing. It should be correct now per http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
Nelson
Exactly what I was looking for! I am using Nothing for the 3rd parameter instead of CultureInfo.InvariantCulture though. I was using the wrong string format "yyyymmdd" as well in my attempts.
NinjaBomb
+3  A: 

Overkill code...

// C#

var ds1 = 20100511;
var ds2 = 20100428;

Func<int, DateTime> getDate = s => DateTime.ParseExact(s.ToString(),
                                                       "yyyyMMdd",
                                                       null);

var d1 = getDate(ds1);
var d2 = getDate(ds2);

var diff = d1.Subtract(d2);
var result = diff.Days; //13

...

//VB.Net

Dim ds1 = 20100511
Dim ds2 = 20100428

Dim getDate = Function(s) DateTime.ParseExact(s.ToString(), "yyyyMMdd", Nothing)

Dim d1 = getDate(ds1)
Dim d2 = getDate(ds2)

Dim diff = d1.Subtract(d2)
Dim result = diff.Days '13
Matthew Whited
to get VB.Net replace the `var` with `Dim` and remove the semicolons
Matthew Whited
+1 for not repeating ParseExact()
Nelson
lol... I just changed it so it was VB... guess I should change it back.
Matthew Whited