Date Vector/Number to ISO 8601 Date String
Easy conversion of a datetime or a date vector or a serial date number into a date string. The date string may be an ISO 8601 timestamp or a single date/time value, selected by (optional) input token/s. Multiple tokens may be used to output multiple strings.
The ISO 8601 timestamp style options supported by this function are:
* Date in calendar, ordinal, or week-numbering notation.
* Basic or extended format.
* Choice of date-time separator character.
* Full or lower precision (fewer trailing date/time units).
* Decimal fraction of the trailing unit (decimal places).
Does NOT parse or support timezones.
By default the function uses the current time and returns the basic ISO 8601 calendar timestamp: this is very useful for naming files that sort alphabetically into chronological order.
### Examples ###
* Using the date+time given by date vector [1999,1,3,15,6,48.0568].
>> datestr8601()
ans = '19990103T150648'
>> datestr8601([],'yn_HM')
ans = '1999003_1506'
>> datestr8601(clock,'*ymdHMS')
ans = '1999-01-03T15:06:48'
>> [D1,D3] = datestr8601(now-2,'D','DDD')
D1 = '5'
D3 = 'Fri'
>> datestr8601(datetime,'DDDD','d*','mmmm','yyyy')
ans = 'Sunday 3rd January 1999'
>> [da,YWD,mmyy] = datestr8601([],'d*','*YWD','mmmm','yyyy');
>> sprintf('The %s of %s has the ISO week-date "%s".',da,mmyy,YWD)
ans = 'The 3rd of January 1999 has the ISO week-date "1998-W53-7".'
### ISO 8601 Date Notations ###
Timestamps are shown here in extended format with the default date-time separator character 'T'.
1) Calendar:
<year>-<month>-<dayofmonth>T<hour>:<minute>:<second>
string: '1999-01-03T15:06:48'
token: '*ymdHMS'
2) Ordinal:
<year>-<dayofyear>T<hour>:<minute>:<second>
string: '1999-003T15:06:48'
token: '*ynHMS'
3) Week-numbering:
<year>-W<weeknumber>-<dayofweek>T<hour>:<minute>:<second>
string: '1998-W53-7T15:06:48'
token: '*YWDHMS'
### Single-Value Strings ###
* Consistent token syntax (UPPERCASE = week-numbering year, lowercase = calendar year).
* Easily adapted to other languages.
* Single-values include: day of the year or days remaining in year; year quarter (3-month or 13-week); year; month as digit or name; day of week as digit or name; date of month; hour; minute; second; deci/centi/milliseconds.
Citation pour cette source
Stephen23 (2024). Date Vector/Number to ISO 8601 Date String (https://www.mathworks.com/matlabcentral/fileexchange/34095-date-vector-number-to-iso-8601-date-string), MATLAB Central File Exchange. Extrait(e) le .
Compatibilité avec les versions de MATLAB
Plateformes compatibles
Windows macOS LinuxCatégories
Tags
Remerciements
Inspiré par : ISO 8601 Date String to Serial Date Number
A inspiré : Words to Number, LCS_v1.39, LCS, Round Dates and Times, ISO 8601 Date String to Serial Date Number, Number to Words
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Découvrir Live Editor
Créez des scripts avec du code, des résultats et du texte formaté dans un même document exécutable.
Version | Publié le | Notes de version | |
---|---|---|---|
2.0.1 | * Add error IDs. |
||
2.0.0 | * Better parsing algorithm.
|
||
1.17.0.0 | * Update online blurb.
|
||
1.16.0.0 | - Downloadable as toolbox |
|
|
1.15.0.0 | - Remove calendar week number (confusing!) |
||
1.14.0.0 | - Improve token input checking and error messages.
|
||
1.13.0.0 | - Removed 'F' from ISO 8601 timestamps: please use decimal fraction instead.
|
||
1.12.0.0 | - Disallow period '.' as date-time separator character. |
||
1.11.0.0 | ISO 8601 dates/timestamps:
|
||
1.9.0.0 | - Allow 'F' (millisecond) in 8601 timestamp tokens. |
||
1.8.0.0 | - Fix bug in Midnight/AM/Noon/PM calculation.
|
||
1.7.0.0 | - Help improved to better contrast calendar vs week-numbering dates.
|
||
1.6.0.0 | - Add day-of-week (as a number) and year (week-date) tokens.
|
||
1.5.0.0 | Add default time (current time).
|
||
1.4.0.0 | - Now accepts Date Number OR Date Vector.
|
||
1.3.0.0 | Added Ordinal Date style, and format control. |
||
1.2.0.0 | Some corrections to Description field and Mfile help comments. |
||
1.0.0.0 |