Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Date fromat question!

Posted on 2002-06-18
2
Medium Priority
?
193 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

721 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