0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

c++ builder > TDateTime > 日付、時分秒の比較 (ミリ秒は無視)

Last updated at Posted at 2015-06-21
動作確認
C++ Builder XE4

2つのTDateTime型変数を比較する際、ミリ秒は多少異なっても日付と時分秒は同じかどうかをチェックするには以下のようにする。

RecodeMilliSecond()を使って、ミリ秒を0に設定した上でCompareDateTime()している。

# include <DateUtils.hpp>
...

static bool comp_dts(TDateTime dt1_, TDateTime dt2_)
{

	TValueRelationship res;
	int nop=1;

	dt1_ = RecodeMilliSecond(dt1_, 0);
	dt2_ = RecodeMilliSecond(dt2_, 0);

	res = CompareDateTime(dt1_, dt2_);
	switch (res) {
	case LessThanValue:
		nop=1;
		break;
	case EqualsValue:
		nop=1;
		break;
	case GreaterThanValue:
		nop=1;
		break;
	default:
		;
	}
}

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	TDateTime dt1, dt2;

	dt1 = StrToDateTime(L"2015/06/21 12:30:45");
	dt2 = StrToDateTime(L"2015/06/21 12:30:48");
	comp_dts(dt1, dt2);

	dt1 = StrToDateTime(L"2015/06/21 12:30:45.002");
	dt2 = StrToDateTime(L"2015/06/21 12:30:45.900");
	comp_dts(dt1, dt2);
}
//---------------------------------------------------------------------------
0
1
0

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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?