Solved

Date fromat question!

Posted on 2002-06-18
2
187 Views
Last Modified: 2010-05-18
I need a function that can read a string and return true if it is in a valid format.  Is there anything in Date.h that can do this?
The string passed in must be in like "MM/DD/YYYY" to pass.
Take a look at the examples and expected results...

"06/18/2002"    Pass.
"18/06/2002"    Fail.
"June/18/2002"  Fail.
"06/18/02"      Fail.

Does C/C++ provide a function that can take a string like this an return the desired result?
0
Comment
Question by:eoincorrigan
2 Comments
 
LVL 4

Accepted Solution

by:
mblat earned 100 total points
ID: 7090109
no, I don't think so....
But it is easy to write.

char* szDate;
char*  token;
char seps[] = "//";
int nMonth = -1,nDay = -1,nYear = -1;

   token = strtok( szDate, seps );
   if(!token)
     return FALSE;
   nMonth = atoi(token);
   if(nMonth < 1 && nMonth > 12)
         return FALSE;

   token = strtok( NULL, seps );
   if(!token)
     return FALSE;
   nDate = atoi(token);
   if(nDate < 1 && nDate > 31)
   // really just a bit more complicated than this last valid    date depends on month and year ( can be leap year!)
         return FALSE;

   token = strtok( NULL, seps );
   if(!token)
     return FALSE;
    nYear = atoi(token);

   // todo: verify year, for example
   if(strlen(token) != 4)
     return FALSE;


Hope it helps...
0
 

Author Comment

by:eoincorrigan
ID: 7090131
I didn't think there was one.
This is just what I was looking for, thanks.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 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

15 Experts available now in Live!

Get 1:1 Help Now