Solved

Date fromat question!

Posted on 2002-06-18
2
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

622 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