user1054513
user1054513

Reputation: 635

C++ check for valid date time format

Whats a good way to check a date format, i want the format to be 2011-12-08 16:59:18 I have boost would using regex be the best way to go or is there some C++ way of doing it. Thanks in advance. here are some test conditions but not limited to these.

So for example someone enters

2011-2-08 16:59:18 //incorrect date month needs to have 2 digits 02
2011-02-08 16:9:18  //incorrect minuets needs to have 2 digits 09
2011-02-0X 16:09:18 //incorect alpha character for day no alpha except - and :
2011-12-08 16:59:18 // correct

Upvotes: 3

Views: 5116

Answers (2)

parapura rajkumar
parapura rajkumar

Reputation: 24403

You can use strptime to convert a date from string format to a tm structure.

Also you can use Boost Date Time Input/Output

Upvotes: 2

Matt K
Matt K

Reputation: 13852

Regular expressions would work, or you could just walk the string and test each character to be sure it is what you expect it to be. You will probably spend less time doing the latter.

Upvotes: 5

Related Questions