LoginSignup
1

More than 3 years have passed since last update.

C++ Builder 10.2 Tokyo > TDateTime > 文字列からの日付変換 > 不正な文字列の確認 > try, catchではなくTryStrToDate()を使う

Posted at
動作環境
RAD Studio 10.2 Tokyo Update 3 
Windows 10 v1903 

概要

日付文字列からTDate(TDateTime)型を作る際のエラーについて。

デバッガエラー

下記の実装の場合、日付文字列が妥当か分かるが、デバッガ動作時にエラーが発生する。

bool __fastcall TForm1::checkValidDate()
{
    TDateTime res;
    try {
        res = VarToDateTime(L"2019/06/31");
    } catch (...) {
        return false;
    }
    return true;
}

情報

TryStrToDate()関数があるようだ

変更後

bool __fastcall TForm1::XXX()
{
    TDateTime adt;
    return TryStrToDate(L"2019/06/31", adt);
}

こちらの実装に変更後、デバッガエラーは出なくなった。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1