フィルターのクリア

How to validate that one datetime is greater than another

17 ビュー (過去 30 日間)
Rich006
Rich006 2024 年 5 月 17 日 15:28
コメント済み: Rich006 2024 年 5 月 20 日 12:12
My class takes two datetimes startTime and endTime as input arguments. I would like to use argument validation to force endTime to be later than startTime, but mustBeGreaterThan does not accept datetimes.

採用された回答

Stephen23
Stephen23 2024 年 5 月 17 日 16:29
編集済み: Stephen23 2024 年 5 月 17 日 16:56
Do NOT convert to low-precision serial date numbers! Avoid deprecated DATENUM !
Argument validation lets you define your own validation function, which makes your task easy (and you get to write exactly the informative error messages that you want to see displayed):
A = datetime(1994,1,1);
B = datetime(2024,1,1);
myfun(A,B)
okay!
myfun(B,A)
Error using solution>myfun (line 9)
Invalid argument at position 2. End time must be after start time.
function myfun(startTime,endTime)
arguments
startTime (1,1) {idt}
endTime (1,1) {idt,ilt(startTime,endTime)}
end
disp('okay!')
end
function idt(x)
assert(isdatetime(x),'Input must be a DATETIME.')
end
function ilt(s,e)
assert(s<e,'End time must be after start time.')
end
  5 件のコメント
Stephen23
Stephen23 2024 年 5 月 17 日 18:49
編集済み: Stephen23 2024 年 5 月 17 日 19:05
Some changes:
  • move the validation functions to local functions (rather than methods),
  • do not use the same name for the method and the class,
  • define the class object as a method input. This requirement is explained here:
After that (see attached file), it works as expected:
A = datetime(1994,1,1);
B = datetime(2024,1,1);
obj = myClass();
obj.myMethod(A,B)
okay!
obj.myMethod(B,A)
Error using myClass/myMethod (line 7)
Invalid argument at position 3. End time must be after start time.
Rich006
Rich006 2024 年 5 月 20 日 12:12
That still isn't what I was trying to do, because it requires running a method after construction. But I did use your suggestion of standalone validation functions. This allows me to create my own mustBeLater. And that answers my question!
classdef myClass
properties
startTime
endTime
end
methods
function obj = myClass(s,e)
arguments
s datetime
e datetime {mustBeLater(e,s)}
end
obj.startTime = s;
obj.endTime = e;
disp('Success!')
end
end
end
function mustBeLater(laterTime,earlierTime)
if laterTime < earlierTime
error("End time must be after start time.")
end
end
>> A = datetime('yesterday');
>> B = datetime('now');
>> obj = myClass(A,B);
Success!
>> obj = myClass(B,A);
Error using myClass
obj = myClass(B,A);
Invalid argument at position 2. End time must be after start time.
(sorry, not sure how to format the inline command inputs/outputs)

サインインしてコメントする。

その他の回答 (2 件)

Joshua Levin Kurniawan
Joshua Levin Kurniawan 2024 年 5 月 17 日 15:41
You can use traditional logical operator for datatime. Operator "<", if true, means that the first datatime will occurs sooner than the next variable. In this case, just use the following code
if startTime < endTime
disp("The data is valid");
else
disp("The data is invalid");
end
For further information, you can check out the following page
  1 件のコメント
Rich006
Rich006 2024 年 5 月 17 日 15:56
I'm trying to do this in argument validation, which is why I was trying to use mustBeGreaterThan.

サインインしてコメントする。


Fangjun Jiang
Fangjun Jiang 2024 年 5 月 17 日 15:45
編集済み: Fangjun Jiang 2024 年 5 月 17 日 15:48
It seems fine in R2022b, R2023b and R2024a
a=now;
b=now;
mustBeGreaterThan(b,a)
mustBeGreaterThan(a,b)
Value must be greater than 739389.6563.
  2 件のコメント
Rich006
Rich006 2024 年 5 月 17 日 15:53
It works with numbers, but I'm trying to compare datetime objects.
Fangjun Jiang
Fangjun Jiang 2024 年 5 月 17 日 16:16
編集済み: Fangjun Jiang 2024 年 5 月 17 日 16:17
okay, I thought now() returns a datetime object. It was not. You can convert it to datenum()
a=datetime('now');
b=datetime('now');
mustBeGreaterThan(datenum(b),datenum(a))
mustBeGreaterThan(datenum(a),datenum(b))
Value must be greater than 739389.6782.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by