Datetime format capatilization sensitivity inconsistent?

10 vues (au cours des 30 derniers jours)
Samuel Cheung
Samuel Cheung le 26 Juil 2024
Commenté : Samuel Cheung le 26 Juil 2024
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 commentaire
Samuel Cheung
Samuel Cheung le 26 Juil 2024
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.

Connectez-vous pour commenter.

Réponse acceptée

Cris LaPierre
Cris LaPierre le 26 Juil 2024
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
  1 commentaire
Samuel Cheung
Samuel Cheung le 26 Juil 2024
Ah, thank you. Didn't realise it's repeating the seconds with sss.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by