Date in format yyyyMMdd

Posted on 2011-10-14
Last Modified: 2012-05-12
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?


Question by:pvg1975
    LVL 7

    Expert Comment

    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

    LVL 7

    Expert Comment

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

    Author Comment

    Hi Slimfinger,

    Line 8 killed me, Im working on VB instead of C#
    LVL 7

    Accepted Solution

    It's trivial to convert it.  Look up TimeSpan on Google - the MSDN doc's usually give examples in different languages.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now