我必須編寫2個函數。將日期作爲字符串並檢查其是否以mm/dd/yy格式顯示;如果它的格式不正確,則應該對其進行編輯。另一個函數應該將驗證日期轉換爲「Month dd,20yy」的格式。C++中的日期驗證和轉換
我很確定我可以照顧第二個功能,但我遇到了第一個問題。我只是不知道如何檢查其格式是否有任何想法?
我認爲這會工作,但它似乎沒有...
更新代碼:
bool dateValidation(string shipDate)
{
string temp;
if(shipDate.length() == 8)
{
if(shipDate[2] == '/' && shipDate[5] =='/')
{
int tempDay, tempMonth, tempYear;
//Gather month
temp = shipDate[0];
temp += shipDate[1];
//convert string to int
tempMonth = temp.atoi;
temp = "";
//Gather day
temp = shipDate[3];
temp += shipDate[4];
//convert string to int
tempDay = temp.atoi;
temp = "";
//Gather year
temp = shipDate[6];
temp += shipDate[7];
//convert string to int
tempYear = temp.atoi;
temp = "";
if(tempMonth > 0 && tempMonth <= 12)
{
if(tempMonth == 9 ||
tempMonth == 4 ||
tempMonth == 6 ||
tempMonth == 11 ||)
{
if(tempDay > 0 && tempDay <= 30)
{
if 30 days
}
}
else if(tempMonth == 2)
{
if(tempDay > 0 && tempDay <= 28)
{
if 28 days
}
}
else
{
if(tempDay > 0 && tempDay <= 31)
{
if 31 days
}
}
}
}
}
}
爲什麼不起作用,是什麼問題?日期還有什麼樣的變化,它可能是這樣的:101112還是它總是在d/m/y之間還有m和d和y可以交換? –
我剛碰到一堵牆。我重複了20次,這是我最後停下的地方。它在這裏不完整。我需要一些幫助來寫它。 :/ –
你實際上沒有驗證這些值是正確的..只是檢查標點符號。 –