Solved

Date fromat question!

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

838 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