date is a library which adds to LME functions to convert date and time between numbers and strings.
The following statement makes available functions defined in date:
use date
Date to string conversion.
use date str = datestr(datetime) str = datestr(date, format)
datestr(datetime) converts the date and time to a string. The input argument can be a vector of 3 to 6 elements for the year, month, day, hour, minute, and second; a julian date as a scalar number; or a string, which is converted by datevec. The result has the following format:
jj-mmm-yyyy HH:MM:SS
where jj is the two-digit day, mmm the beginning of the month name, yyyy the four-digit year, HH the two-digit hour, MM the two-digit minute, and SS the two-digit second.
The format can be specified with a second input argument. When datestr scans the format string, it replaces the following sequences of characters and keeps the other ones unchanged:
Sequence | Replaced with |
---|---|
dd | day (2 digits) |
ddd | day of week (3 char) |
HH | hour (2 digits, 01-12 or 00-23) |
MM | minute (2 digits) |
mm | month (2 digits) |
mmm | month (3 char) |
PM | AM or PM |
quarter (Q1 to Q4) | |
SS | second (2 digits) |
sss | fraction of second (1-12 digits) |
yy | year (2 digits) |
yyyy | year (4 digits) |
If the sequence PM is found, the hour is between 1 and 12; otherwise, between 0 and 23. Second fraction has as many digits as there are 's' characters in the format string.
use date datestr(clock) 18-Apr-2005 16:21:55 datestr(clock, 'ddd mm/dd/yyyy HH:MM PM') Mon 04/18/2005 04:23 PM datestr(clock, 'yyyy-mm-ddTHH:MM:SS,sss') 2008-08-23T02:41:37,515
String to date and time conversion.
use date datetime = datevec(str)
datevec(str) converts the string str representing the date and/or the time to a row vector of 6 elements for the year, month, day, hour, minute, and second. The following formats are recognized:
Example | Value |
---|---|
20050418T162603 | ISO 8601 date and time |
2005-04-18 | year, month and day |
2005-Apr-18 | year, month and day |
18-Apr-2005 | day, month and year |
04/18/2005 | month, day and year |
04/18/00 | month, day and year |
18.04.2005 | day, month and year |
18.04.05 | day, month and year |
16:26:03 | hour, minute and second |
16:26 | hour and minute |
PM | afternoon |
Unrecognized characters are ignored. If the year is given as two digits, it is assumed to be between 1951 and 2050.
use date datevec('Date and time: 20050418T162603') 2005 4 18 16 26 3 datevec('03:57 PM') 0 0 0 15 57 0 datevec('01-Aug-1291') 1291 8 1 0 0 0 datevec('At 16:30 on 11/04/07') 2007 11 4 16 30 0
Week day of a given date.
use date (num, str) = weekday(year, month, day) (num, str) = weekday(datetime) (num, str) = weekday(jd)
weekday finds the week day of the date given as input. The date can be given with three input arguments for the year, the month and the day, or with one input argument for the date or date and time vector, or julian date.
The first output argument is the number of the day, from 1 for Sunday to 7 for Saturday; and the second output argument is its name as a string of 3 characters, such as 'Mon' for Monday.
Day of week of today:
use date (num, str) = weekday(clock) num = 2 str = Mon