Easy conversion of 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).
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].
ans = '19990103T150648'
ans = '1999003_1506'
ans = '1999-01-03T15:06:48'
>> [D1,D3] = datestr8601(now-2,'D','DDD')
D1 = '5'
D3 = 'Fri'
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'.
### Single-Value Strings ###
* One date/time value per output string (note: can be joined afterwards, e.g. using SPRINTF).
* 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.
Stephen Cobeldick (2020). 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. Retrieved .
@Tago: you seem to be trying to use DATESTR8601 as if it was MATLAB's inbuilt DATESTR. In actual fact DATESTR8601 is not the same function as the inbuilt DATESTR: they are different functions with different syntaxes and different documentation. You should try reading the DATESTR8601 documentation, as then you would know how to use it.
Both of your cases are easy to achieve:
ans = 20181117T144652.899
ans = 20181117
Sometimes simple to use, but not working fine. Simple requests like:
Thanks a lot. A very useful subroutine.
* Better parsing algorithm.
* Update online blurb.
- Downloadable as toolbox
- Remove calendar week number (confusing!)
- Improve token input checking and error messages.
- Removed 'F' from ISO 8601 timestamps: please use decimal fraction instead.
- Disallow period '.' as date-time separator character.
ISO 8601 dates/timestamps:
- Allow 'F' (millisecond) in 8601 timestamp tokens.
- Fix bug in Midnight/AM/Noon/PM calculation.
- Help improved to better contrast calendar vs week-numbering dates.
- Add day-of-week (as a number) and year (week-date) tokens.
Add default time (current time).
- Now accepts Date Number OR Date Vector.
Added Ordinal Date style, and format control.
Some corrections to Description field and Mfile help comments.
Inspired by: ISO 8601 Date String to Serial Date Number