Checking the dates and times
11 次查看(过去 30 天)
显示 更早的评论
I am having some trouble with developing code to see if the date inputted for the Testdate is one week old and if the date inputted for the Testdate isn't the current date for today should show an input for an error message to confirm the Testdate they inputted. So far I have:
Testdate=input('What is the test date?(dd-mm-yyyy)','s')
Teststr=convertCharsToStrings(Testdate);
TestT=ERRORT(Testdate);
Then here is the function with the error checking code:
if
Testdate=input('Confirm this test data: Y or N','s')
end
So I also would like if the user inputs Y, it continues to store the value and continue through the main script, but if inputs N, it allows the user to input a different Testdate to store.
Any suggestions or resolutions?
采纳的回答
Adam Danz
2020-7-21
Why not use a UI such as
- https://www.mathworks.com/help/finance/uicalendar-graphical-user-interface.html
- https://www.mathworks.com/help/matlab/ref/uidatepicker.html
Using your method, the date should be converted to datetime
Testdate=input('What is the test date (dd-mm-yyyy)? ','s');
TestdateDT = datetime(Testdate,'InputFormat','dd-MM-yyyy');
Then test if the date is within 1 week (True/False) and is equal to today (True/False)
isWithinOneWeek = days((datetime('now') - TestdateDT)) <=7;
isToday = isequal(datetime('today'), TestdateDT);
If you want the user to continually enter a date until it obeys the rules, put that within a while loop.
Modify this to your needs
isWithinOneWeek = false;
while ~isWithinOneWeek
Testdate=input('What is the test date (dd-mm-yyyy)? ','s');
TestdateDT = datetime(Testdate,'InputFormat','dd-MM-yyyy');
isWithinOneWeek = days((datetime('now') - TestdateDT)) <=7;
end
One big flaw with this method is that it relys on the user to obey the command. For example, if the user uses slashes instead of dashes, you'll get an error: 21/07/2020.
This is why it's better to use more controlled methods of user input.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!