Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Suppose the user inserts an integer n and a date string '1/1/2015' (stands for Jan 1 2015), then how do I find the date that is n days before Jan 1 2015?

1 回表示 (過去 30 日間)
Shawn Miller
Shawn Miller 2015 年 12 月 17 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
It seems minus sign doesn't work. Any date function can help?
  1 件のコメント
jgg
jgg 2015 年 12 月 17 日
A string does not work with the minus sign, as you'd expect. You need to convert the string to a number, subtract your n, then convert it back to a date string to output. Take a look at Matlab's date functions to see how to do this.

回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 12 月 18 日
d = datetime('1/1/2015', 'InputFormat', 'M/d/Y');
d - days(n)
  2 件のコメント
Shawn Miller
Shawn Miller 2015 年 12 月 24 日
Thanks, it seems it should be y rather than Y. Also, the result returned from d - days(n) also includes time. Anyway, it partially works.
Walter Roberson
Walter Roberson 2015 年 12 月 24 日
d = datetime('1/1/2015', 'InputFormat', 'M/d/y', 'Format', 'M/d/y');
d - days(n)

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by