Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ check if a date is valid

Tags:

c++

date

time

is there any function to check if a given date is valid or not? I don't want to write anything from scratch.

e.g. 32/10/2012 is not valid and 10/10/2010 is valid

like image 496
MBZ Avatar asked Feb 24 '12 19:02

MBZ


People also ask

How check DateTime is valid or not in C#?

Use the DateTime. TryParseExact method in C# for Date Format validation. They method converts the specified string representation of a date and time to its DateTime equivalent. It checks whether the entered date format is correct or not.

How do you check if a date is valid in typescript?

If the variables are of Date object type, we will check whether the getTime() method for the date variable returns a number or not. The getTime() method returns the total number of milliseconds since 1, Jan 1970. If it doesn't return the number, it means the date is not valid.

How do you validate a date in YYYY-MM-DD format in Java?

You need to set lenient false (setLenient(false);) for simple date format otherwise sometimes results will be invalid for example yyyy-mm-dd will get parse with wrong date.


1 Answers

If the format of the date is constant and you don't want to use boost, you can always use strptime, defined in the ctime header:

const char date1[] = "32/10/2012";
const char date2[] = "10/10/2012";
struct tm tm;

if (!strptime(date1, "%d/%m/%Y", &tm)) std::cout << "date1 isn't valid\n";
if (!strptime(date2, "%d/%m/%Y", &tm)) std::cout << "date2 isn't valid\n";
like image 194
netcoder Avatar answered Oct 24 '22 04:10

netcoder