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.

Dani AI

Generated

Short summary and practical options (fills the gaps left by the replies from and ).

COleDateTime::ParseDateTime is a convenient built‑in for MFC, but it is permissive and locale-sensitive — it uses OLE conversion routines internally and can accept several different date orders instead of strictly enforcing one format. For strict, format-specific validation do not rely on ParseDateTime alone. (learn.microsoft.com)

If the requirement is "validate a known format" (for example mm/dd/yyyy), a simple, robust approach is:

  • parse the string with an explicit format,
  • normalize/round‑trip the components via the standard C/C++ library,
  • compare the normalized result to the original components (mismatch => invalid).

A concise implementation uses std::get_time to fill a std::tm, then std::mktime to normalize and detect overflow/rollover. Example (validate mm/dd/YYYY):

#include <sstream>
#include <iomanip>
#include <ctime>

bool is_valid_mmddyyyy(const std::string& s) {
    std::tm tm = {};
    std::istringstream ss(s);
    ss >> std::get_time(&tm, "%m/%d/%Y");
    if (ss.fail()) return false;
    int oy = tm.tm_year, om = tm.tm_mon, od = tm.tm_mday;
    tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
    tm.tm_isdst = -1;
    if (std::mktime(&tm) == -1) return false;
    return (tm.tm_year == oy && tm.tm_mon == om && tm.tm_mday == od);
}

(Details: std::get_time / time_get parsing and std::mktime normalization behavior are documented in the standard references.) (en.cppreference.com)

For modern codebases prefer C++20 calendar types (std::chrono::year_month_day with from_stream / ok()) or Boost.DateTime for older toolchains — they provide direct validity checks and clearer semantics for leap years and month lengths. (en.cppreference.com)

Practical tips: prefer an explicit format when user input is free text (to avoid dd/mm vs mm/dd ambiguity), avoid two‑digit years, and for UI input use a DateTime control or masked entry so parsing is unnecessary. If staying with COleDateTime, pre-validate the format before calling ParseDateTime when strictness is required. (learn.microsoft.com)

Recommended Answers

All 6 Replies

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.

Thanks

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;
}

Stop spamming old threads, you asshole!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.