How to convert time format of file date
8 次查看(过去 30 天)
显示 更早的评论
Hello, I want to convert the date string obtained by file.date to the format yyyymmdd_HHMMSS, i.e.,
'13-Mrz-2023 09:19:45'
to
'20230313_091945'.
I want to make the time being UTC and the converion applicable for all system languages (de_DE, en_US, etc.), i.e., system independent.
I'm struggling with the locale parameter ('Mrz' might be 'Mar' in English languages).
The MATLAB version is 2017b.
Thank you for your help.
0 个评论
采纳的回答
Stephen23
2023-3-13
编辑:Stephen23
2023-3-13
"Hello, I want to convert the date string obtained by file.date to the format yyyymmdd_HHMMSS, i.e.,"
A much simpler and much more robust approach is to use the DATENUM field (assuming that you are working with the output from the DIR function). Then you can trivially avoid all of the hassle of different date locales. For example:
writematrix(pi,'myfile.txt')
S = dir('*.txt') % Use the DATENUM field, not the locale-dependent DATE field!
T = datetime(S(1).datenum, 'ConvertFrom','datenum', 'Format','uuuuMMdd_HHmmss', 'TimeZone','local')
T.TimeZone = 'Z' % UTC
Apparently this forum uses UTC, but if your locale is set up correctly, then changing the TimeZone will adjust the time&date automatically.
更多回答(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!