How to convert datetime into a number?

I have a table whose first column is a timestamp in the format 08-Jan-2016 05:40:00. How do I convert it into numbers?
Wanted output: 20160108054000.
I know the function yyyymmdd(X) is useful but I also need the time to be added.
Thanks!

Réponses (2)

dt = datestr(now,'yyyymmddHHMMSS')
dt = '20220403132156'

5 commentaires

Lu Da Silva
Lu Da Silva le 3 Avr 2022
Modifié(e) : Lu Da Silva le 3 Avr 2022
How can I implement that onto my table though? Replacing 'now' with my vector gives an error ("Cannot convert input into specified datestr. DATENUM failed.")
KSSV
KSSV le 3 Avr 2022
What is class of your dates in the table?
Lu Da Silva
Lu Da Silva le 3 Avr 2022
what do you mean by class? Where do I check that?
The format is e.g. 08-Jan-2016 05:40:00.
Siddharth Bhutiya
Siddharth Bhutiya le 5 Avr 2022
Avoid using older legacy functions like datestr and datenum. Use the more modern types like datetime to work with timestamped data.
@Lu Da Silva Could you share a code example to demonstrate what you are seeing. This way people can run that on their MATLAB and suggest possible solutions instead of trying to guess what the problem could be.
Boyd Tolton
Boyd Tolton le 6 Août 2024
Although datetime does provide more precision than datenum (and requires more memory), this precision is not needed by most applications. Also, you will note that the dir command outputs the datenum for files.

Connectez-vous pour commenter.

Stephen23
Stephen23 le 6 Août 2024
Modifié(e) : Stephen23 le 6 Août 2024
txt = '08-Jan-2016 05:40:00'
txt = '08-Jan-2016 05:40:00'
dtn = datetime(txt,'InputFormat','d-MMM-y H:m:s', 'Format','yyyyMMddHHmmss')
dtn = datetime
20160108054000

Catégories

Modifié(e) :

le 6 Août 2024

Community Treasure Hunt

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

Start Hunting!

Translated by