Clear Filters
Clear Filters

Datetime format capatilization sensitivity inconsistent?

22 views (last 30 days)
So I was trying to add milliseconds to my datetime variables and found a sort of inconsistency?
When setting datetime format using datetime('now','Format','hh:mm:ss.sss') , the sss at the end is not capatilization sensitive; but when trying to access and change it using variable_name.Format = 'hh:mm:ss.SSS', the SSS at the end is capatilization sensitive.
Is this a minor inconsitency or is this intended? If it's the latter what is the rationale behind? As it only makes it more confusing imo
  1 Comment
Samuel Cheung
Samuel Cheung on 26 Jul 2024 at 14:42
Hi Himanshu,
I am using 2022a. variable_name.Format = 'hh:mm:ss.sss' is not valid and would yield an error. The issue has now been resolved by the staff below and they provided a good explanation of what sss is doing so you could take a look if you are interested.

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 26 Jul 2024 at 14:22
It is case-sensitive in both contexts. Either way, you get a result with 3 decimal places, but notice that the result is just a repeat of the seconds if you use 'sss'.
T1 = datetime('now','Format','hh:mm:ss.sss')
T1 = datetime
02:21:38.038
T1 = datetime('now','Format','hh:mm:ss.SSS')
T1 = datetime
02:21:38.975
This is also true when using variable_name.Format = 'hh:mm:ss.SSS'
T2 = datetime('now');
T2.Format = 'hh:mm:ss.sss'
T2 = datetime
02:21:38.038
T2.Format = 'hh:mm:ss.SSS'
T2 = datetime
02:21:38.984
In short, you must use 'SSS' if you want to include milliseconds

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!