DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   C++ / MFC Date Validation (http://www.daniweb.com/forums/thread158778.html)

koushal.vv Nov 21st, 2008 9:18 am
C++ / MFC Date Validation
 
Hi,


i just need to know is there any method in MFC / C++
which will do the validation for the entered date.
ie if the date 12/11/2008 is given as input it should return saying the date is valid.i hope there exsists some standard function.
it should also check for leap leap year,and wrong date such as 31/11/2008 etc. thanks in advance.

niek_e Nov 21st, 2008 9:34 am
Re: C++ / MFC Date Validation
 
you could try the parsedate() member of COleDateTime:
if (!COleDateTime::ParseDateTime("12/11/2008")
{
    // That's not a date bucko!
}

I don't have MFC anymore, so I can't test it, but I'm sure it works somewhat like that. If this doesn't work you should have a look at the wxDateTime object from wxWidgets.

koushal.vv Nov 22nd, 2008 3:40 pm
Re: C++ / MFC Date Validation
 
Thanks

blacklight332 Mar 3rd, 2009 10:16 pm
Re: C++ / MFC Date Validation
 
C works in MFC right? (sprintf/sscanf)
int main(int argc, char*argv[])
{
    int m,d,y;
    sscanf("%d/%d/%d", &m, &d, &y);
    if (checkdate(m,d,y))
    {
        printf("valid\n");
    }
    else
    {
        printf("invalid\n");
    }
}


bool checkdate(int m, int d, int y)
{
  //gregorian dates started in 1582
  if (! (1582<= y )  )//comment these 2 lines out if it bothers you
    return false;
  if (! (1<= m && m<=12) )
    return false;
  if (! (1<= d && d<=31) )
    return false;
  if ( (d==31) && (m==2 || m==4 || m==6 || m==9 || m==11) )
    return false;
  if ( (d==30) && (m==2) )
    return false;
  if ( (m==2) && (d==29) && (y%4!=0) )
    return false;
  if ( (m==2) && (d==29) && (y%400==0) )
    return true;
  if ( (m==2) && (d==29) && (y%100==0) )
    return false;
  if ( (m==2) && (d==29) && (y%4==0)  )
    return true;

  return true;
}

MosaicFuneral Mar 3rd, 2009 10:17 pm
Re: C++ / MFC Date Validation
 
Stop spamming old threads, you asshole!

blacklight332 Mar 3rd, 2009 10:29 pm
Re: C++ / MFC Date Validation
 
@MosaicFuneral

What are you talking about? Isn't this a CODE FORUM. So posting CODE on a CODE FORUM is okay right? Or did I miss the posting guidelines.

John A Mar 3rd, 2009 10:32 pm
Re: C++ / MFC Date Validation
 
>Or did I miss the posting guidelines.
Yes you did. And it's appropriately titled "Read This Before Posting":
http://www.daniweb.com/forums/thread78223.html


All times are GMT -4. The time now is 5:07 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC