Solved

Date fromat question!

Posted on 2002-06-18
2
190 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

735 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