Link to home
Start Free TrialLog in
Avatar of pvg1975
pvg1975Flag for Argentina

asked on

Date in format yyyyMMdd

Hello all,

I have a legacy database from where I retrieve a field (INT) that is supposed to be a date, in the format yyyyMMdd, for example 20111014 for today.

What I need to know is if that date, let's say...

Dim DBdate as integer = 20111014

... is in between today minus 31 days range.

Is this possible?

Thanks!

Paula
Avatar of Bill Nolan
Bill Nolan
Flag of United States of America image

Kind of silly for the dates to be stored that way.  Anyhow, use DateTime and TimeSpan.  You have to parse the int, something like this:


const int SillyDate = 20111014;

int year = SillyDate / 10000;
int month = (SillyDate - year) / 100;
int day = month % 1000;

DateTime date = new DateTime(year, month, day);
TimeSpan span = new TimeSpan(31, 0, 0, 0);

if (DateTime.Now - span)
{
   // bingo
}

Open in new window

Change line 10 above to:
if (date > DateTime.Now - span && date <= DateTime.Now)

...technically, you probably should also set a local var dateNow instead of using the .Now property twice.

(Still don't know why I can't edit a post...)
Avatar of pvg1975

ASKER

Hi Slimfinger,

Line 8 killed me, Im working on VB instead of C#
ASKER CERTIFIED SOLUTION
Avatar of Bill Nolan
Bill Nolan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial