CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Compare Dates

  1. #1
    Join Date
    Apr 1999
    Location
    Lenexa, Kansas
    Posts
    7

    Compare Dates

    Hi,
    I retrieve a value from a Date field in my database. I need to compare this value with the current date. How can I do this without using MFC?
    Thanks,
    Therese


  2. #2
    Guest

    Re: Compare Dates

    Let's say you've done something like this to retrieve your date value in your database :

    COleVariant oleDateValue;
    CDAORecordset daoRecordSet;
    oleDateValue = daoRecordSet.GetFieldValue("DATE_COL");

    To compare with the current date, you can do this :

    COleDateTime oleCurrentDate;
    oleCurrentDate = COleDateTime::GetCurrentTime();
    if (oleCurrentDate == oleDateValue)
    {
    // Both dates are identical
    }
    else
    {
    // The dates are different
    }

    The COleDateTime class have some great methods to manipulate dates and time. Have a look at it in the help.


  3. #3
    Join Date
    Apr 1999
    Location
    Melbourne, VIC
    Posts
    72

    Re: Compare Dates

    ...but without using MFC you'd maybe want to access the underlying DATE data type? If you aren't using MFC at all you won't have COleVariant - will you have VARIANT though? You wont have the MFC DAO wrappers - will you have DAO though? Or are you using ADO? OLEDB? More of a question than an answer really, sorry - but clarify, what are you trying to do?


  4. #4
    Join Date
    Apr 1999
    Location
    Melbourne, VIC
    Posts
    72

    Re: Compare Dates

    There is a Win32 structure SYSTEMTIME and many API's to get the time :

    GetLocalTime(SYSTEMTIME);
    GetSystemTime(SYSTEMTIME);
    GetSystemTimeAdjustment(see help);
    GetTimeZoneInformation(TIME_ZONE_INFORMATION);
    ...


    You can look up the structures and methods in the MSDN, here's the SYSTEMTIME though:-
    [ccode]
    typedef struct _SYSTEMTIME {
    WORD wYear;
    WORD wMonth;
    WORD wDayOfWeek;
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
    } SYSTEMTIME;
    [ccode]
    HTH



  5. #5
    Join Date
    Apr 1999
    Location
    Lenexa, Kansas
    Posts
    7

    Re: Compare Dates

    I am using ADO for data access (via the #import). Right now I am accomplishing the comparison by parsing the date as if it were a string and comparing its "parts" to properties of a system time object.
    This is pretty clumsy, so I am still open for suggestions.

    Therese


  6. #6
    Join Date
    Jun 2001
    Location
    India
    Posts
    5

    Re: Compare Dates

    Better way i have used to find the difference between two dates is to call DATEDIFF function from the backend

    Syntax
    DATEDIFF(datepart, startdate, enddate)

    Returns the number of date and time boundaries crossed between two specified dates.

    e.g.,
    SELECT DATEDIFF(day, pubdate, getdate()) AS no_of_days

    - getdate() will give u the current date




    Everything that has a begining in time must have end in time.

  7. #7
    Join Date
    Nov 2002
    Posts
    57

    How to get the difference between two variables of type DATE !!

    hi,

    I need to get the difference (in minute) between two variables of type DATE.
    Example:


    Code:
    DATE d1; //   12/12/2002 12:10:20 AM
    DATE d2; //   11/12/2002 11:10:20 AM
    Thanks in advance.
    Newbie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured