Check for incorrect argument data type or missing argument in call to function 'dateshift'.

I have a function that calculates left hours till noon
input time: 18:56:19
output hours till noon: 17:03:41
Rounded hours left till noon, output should be 17:00:00
dateshift('17:03:41', 'start', 'hour', 'nearest');
Output: 17:00:00
But when my input hours are less 12 hours it throughs an error:
input time: 06:01:31
output hours till noon: 05:58:29
Rounded hours left till noon, output should be 06:00:00
dateshift('06:01:31', 'start', 'hour', 'nearest');
Check for incorrect argument data type or missing argument in call to function 'dateshift'.
Error in hoursLeftTillNoon (line 64)
hoursLeft = dateshift(hoursLeft, 'start', 'hour', 'nearest');
hoursLeft = output hours till noon

回答 (1 件)

the cyclist
the cyclist 2021 年 11 月 16 日
編集済み: the cyclist 2021 年 11 月 16 日
I get the error from your first statement as well:
% This statement would give an error
% dateshift('17:03:41', 'start', 'hour', 'nearest')
But not if I use a datetime as input:
dateshift(datetime('17:03:41'), 'start', 'hour', 'nearest')
ans = datetime
16-Nov-2021 17:00:00

4 件のコメント

yes, i have it in a datetime in my function.
time = datetime(time, 'Format', 'HH:mm:ss');
noon = datetime('12:00:00', 'Format', 'HH:mm:ss');
hoursLeft = noon - time;
hoursLeft = dateshift(hoursLeft, 'start', 'hour', 'nearest');
I get an error only when my hoursLeft are equal to '05:58:29' not when '17:03:41'. I was thinking that 0 upfront is causing an error.
Stephen23
Stephen23 2021 年 11 月 16 日
編集済み: Stephen23 2021 年 11 月 16 日
time = datetime('now', 'Format', 'HH:mm:ss');
noon = datetime('12:00:00', 'Format', 'HH:mm:ss');
hoursLeft = noon - time;
hoursLeft = dateshift(hoursLeft, 'start', 'hour', 'nearest');
Check for incorrect argument data type or missing argument in call to function 'dateshift'.
Subtracting two DATETIME objects returns a DURATION object.
DATESHIFT is only defined for a DATETIME input argument, not a DURATION input argument.
Therefore as the error message correctly states, the input data type is incorrect.
You should make an enhancement request that DATESHIFT should accept DURATION objects (TIMESHIFT?).
Palina Pauliuchenka
Palina Pauliuchenka 2021 年 11 月 16 日
But why it works when my time is 17:03:34 later rounded to 17:00:00 but not with 05:58:29? I don't understand.
the cyclist
the cyclist 2021 年 11 月 16 日
編集済み: the cyclist 2021 年 11 月 16 日
I tried to replicate an example where hoursLeft is 17:03:01 using your code:
time1 = datetime('02:56:19', 'Format', 'HH:mm:ss');
time2 = datetime('20:00:00', 'Format', 'HH:mm:ss');
hoursLeft = time2 - time1;
hoursLeft = dateshift(hoursLeft, 'start', 'hour', 'nearest');
Check for incorrect argument data type or missing argument in call to function 'dateshift'.
I believe this fails because hoursLeft is a duration, not a datetime.
So, I'm sorry, but I'm still confused about the exact code that does work, and the code that does not. Can you please provide a complete example of each case, that we can run?

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

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

製品

リリース

R2021b

質問済み:

2021 年 11 月 16 日

編集済み:

2021 年 11 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by