Checking the dates and times
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Alexandra Philip
il 21 Lug 2020
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?
2 Commenti
Risposta accettata
Adam Danz
il 21 Lug 2020
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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Dates and Time in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!