Если все, что вам нужно, чтобы проверить, есть ли у две строки же дата или нет, и если это гарантирует, что строки в том же формате , то нет необходимости преобразовывать его в дату. Вам просто нужно сравнить подстроки после первого символа пробела. Если они одинаковые, то даты совпадают. Вот пример кода:
using namespace std;
string getCurrentDate()
{
//Enumeration of the months in the year
const char* months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
//Get the current system date time
SYSTEMTIME st;
GetSystemTime(&st);
//Construct the string in the format "21:5 Jan 23, 11"
ostringstream ss;
ss<<st.wHour<<":"<<st.wMinute<<
" "<<months[st.wMonth-1]<<
" "<<st.wDay<<", "<<st.wYear%1000;
//Extract the string from the stream
return ss.str();
}
string getDateString(const string& s)
{
//Extract the date part from the string "21:5 Jan 23, 11"
//Look for the first space character in the string
string date;
size_t indx = s.find_first_of(' ');
if(indx != string::npos) //If found
{
//Copy the date part
date = s.substr(indx + 1);
}
return date;
}
bool isCurrentDate(const string& s1)
{
//Get the date part from the passed string
string d1 = getDateString(s1);
//Get the date part from the current date
string d2 = getDateString(getCurrentDate());
//Check whether they match
return ! d1.empty() && ! d2.empty() && d1 == d2;
}
int main(void)
{
bool s = isCurrentDate("21:5 Jan 23, 11");
bool s1 = isCurrentDate("21:5 Jan 25, 11");
return 0;
}
хорошее предложение (и более высокую производительность, чем преобразование все строки в дату/время), но следует также добавить, что ему нужно преобразовать 'текущий date' в строку в том же формате, для того, чтобы сравнить – davka
Спасибо Аше для ответа. Но мне нужно сравнить «21: 5 23 января, 11» с текущей датой. Я думаю, что текущее время-дата всегда будет в миллисекундах. Как мне сравнить? – sid
@ Субрат: см. Обновленный код, чтобы получить текущую дату. – Asha