Thursday, July 30, 2009

Current date string

date

Current date string

Syntax

  • str = date

Description

str = date returns a string containing the date in dd-mmm-yyyy format.





MATLAB Function Reference
datenum

Serial date number

Syntax

  • N = datenum(DT)
    N = datenum(DT,P)
    N = datenum(Y,M,D)
    N = datenum(Y,M,D,H,MI,S)

Description

The datenum function converts date strings and date vectors (defined by datevec) into serial date numbers. Date numbers are serial days elapsed from some reference date. By default, the serial day 1 corresponds to 1-Jan-0000.

N = datenum(DT) converts the date string or date vector DT into a serial date number. Date strings with two-character years, e.g., 12-june-12, are assumed to lie within the 100-year period centered about the current year.

    Note If DT is a string, it must be in one of the date formats 0, 1, 2, 6, 13, 14, 15, 16, or 23 as defined by datestr.

N = datenum(DT,P) uses the specified pivot year as the starting year of the 100-year range in which a two-character year resides. The default pivot year is the current year minus 50 years.

N = datenum(Y,M,D) returns the serial date number for corresponding elements of the Y, M, and D (year, month, day) arrays. Y, M, and D must be arrays of the same size (or any can be a scalar). Values outside the normal range of each array are automatically "carried" to the next unit.

N = datenum(Y,M,D,H,MI,S) returns the serial date number for corresponding elements of the Y, M, D, H, MI, and S (year, month, day, hour, minute, and second) array values. Y, M, D, H, MI, and S must be arrays of the same size (or any can be a scalar). Values outside the normal range of each array are automatically carried to the next unit (for example month values greater than 12 are carried to years). Month values less than 1 are set to be 1. All other units can wrap and have valid negative values.

Examples

Convert a date string to a serial date number.

  • n = datenum('19-May-2001')

    n =
    730990

Specifying year, month, and day, convert a date to a serial date number.

  • n = datenum(2001,12,19)

    n =
    731204

Convert a date vector to a serial date number.

  • format bank
    n = datenum([2001 5 19 18 0 0])

    n =
    730990.75

Convert a date string to a serial date number using the default pivot year

  • n = datenum('12-june-12')

    n =
    735032

Convert the same date string to a serial date number using 1900 as the pivot year.

  • n = datenum('12-june-12', 1900)

    n =
    698507

datestr

Date string format

Syntax

  • str = datestr(DT,dateform)
    str = datestr(DT,dateform,P)

Description

The datestr function converts serial date numbers (defined by datenum) and date vectors (defined by datevec) into date strings.

str = datestr(DT,dateform) converts a single date vector, or each element of an array of serial date numbers to a date string. Date strings with two-character years, e.g., 12-june-12, are assumed to lie within the 100-year period centered about the current year.

str = datestr(DT,dateform,P) uses the specified pivot year as the starting year of the 100-year range in which a two-character year resides. The default pivot year is the current year minus 50 years.

The optional argument dateform specifies the date format of the result. dateform can be either a number or a string:


dateform
(number)

dateform (string)
Example
0
'dd-mmm-yyyy HH:MM:SS'
01-Mar-2000 15:45:17
1
'dd-mmm-yyyy'
01-Mar-2000
2
'mm/dd/yy'
03/01/00
3
'mmm'
Mar
4
'm'
M
5
'mm'
03
6
'mm/dd'
03/01
7
'dd'
01
8
'ddd'
Wed
9
'd'
W
10
'yyyy'
2000
11
'yy'
00
12
'mmmyy'
Mar00
13
'HH:MM:SS'
15:45:17
14
'HH:MM:SS PM'
3:45:17 PM
15
'HH:MM'
15:45
16
'HH:MM PM'
3:45 PM
17
'QQ-YY'
Q1-01
18
'QQ'
Q1
19
'dd/mm'
01/03
20
'dd/mm/yy'
01/03/00
21
'mmm.dd.yyyy HH:MM:SS'
Mar.01,2000 15:45:17
22
'mmm.dd.yyyy'
Mar.01.2000
23
'mm/dd/yyyy'
03/01/2000
24
'dd/mm/yyyy'
01/03/2000
25
'yy/mm/dd'
00/03/01
26
'yyyy/mm/dd'
2000/03/01
27
'QQ-YYYY'
Q1-2001
28
'mmmyyyy'
Mar2000
29 (ISO 8601)
'yyyy-mm-dd'
2000-03-01
30 (ISO 8601)
'yyyymmddTHHMMSS'
20000301T154517
31
'yyyy-mm-dd HH:MM:SS'
2000-03-01 15:45:17

    NOTE dateform numbers 0, 1, 2, 6, 13, 14, 15, 16, and 23 produce a string suitable for input to datenum or datevec. Other date string formats will not work with these functions.
Time formats like 'h:m:s', 'h:m:s.s', 'h:m pm', ... can also be part of the input array DT. If you do not specify dateform, or if you specify dateform as -1, the date string format defaults to


1
if DT contains date information only, e.g., 01-Mar-1995
16
if DT contains time information only e.g., 03:45 PM
0
if DT is a date vector, or a string that contains both date and time information e.g., 01-Mar-1995 03:45



datetick

Label tick lines using dates

Syntax

  • datetick(tickaxis)
    datetick(tickaxis,dateform)

Description

datetick(tickaxis) labels the tick lines of an axis using dates, replacing the default numeric labels. tickaxis is the string 'x', 'y', or 'z'. The default is 'x'. datetick selects a label format based on the minimum and maximum limits of the specified axis.

datetick(tickaxis,dateform) formats the labels according to the integer dateform (see table). To produce correct results, the data for the specified axis must be serial date numbers (as produced by datenum).


dateform (number)
dateform (string)
Example
0
'dd-mmm-yyyy HH:MM:SS'
01-Mar-2000 15:45:17
1
'dd-mmm-yyyy'
01-Mar-2000
2
'mm/dd/yy'
03/01/00
3
'mmm'
Mar
4
'm'
M
5
'mm'
03
6
'mm/dd'
03/01
7
'dd'
01
8
'ddd'
Wed
9
'd'
W
10
'yyyy'
2000
11
'yy'
00
12
'mmmyy'
Mar00
13
'HH:MM:SS'
15:45:17
14
'HH:MM:SS PM'
3:45:17 PM
15
'HH:MM'
15:45
16
'HH:MM PM'
3:45 PM
17
'QQ-YY'
Q1-01
18
'QQ'
Q1
19
'dd/mm'
01/03
20
'dd/mm/yy'
01/03/00
21
'mmm.dd.yyyy HH:MM:SS'
Mar.01,2000 15:45:17
22
'mmm.dd.yyyy'
Mar.01.2000
23
'mm/dd/yyyy'
03/01/2000
24
'dd/mm/yyyy'
01/03/2000
25
'yy/mm/dd'
00/03/01
26
'yyyy/mm/dd'
2000/03/01
27
'QQ-YYYY'
Q1-2001
28
'mmmyyyy'
Mar2000

Remarks

datetick calls datestr to convert date numbers to date strings.

To change the tick spacing and locations, set the appropriate axes property (i.e., XTick, YTick, or ZTick) before calling datetick.

Example

Consider graphing population data based on the 1990 U.S. census:

  • t = (1900:10:1990)';     % Time interval
    p = [75.995 91.972 105.711 123.203 131.669 ...
    150.697 179.323 203.212 226.505 249.633]'; % Population
    plot(datenum(t,1,1),p) % Convert years to date numbers and plot
    grid on
    datetick('x',11) % Replace x-axis ticks with 2-digit year labels




datevec

Date components

  • C = datevec(A)
    C = datevec(A,P)
    [Y,M,D,H,MI,S] = datevec(A)

Description

C = datevec(A) splits its input into an n-by-6 array with each row containing the vector [Y,M,D,H,MI,S]. The first five date vector elements are integers. Input A can either consist of strings of the sort produced by the datestr function, or scalars of the sort produced by the datenum and now functions. Date strings with two-character years, e.g., 12-june-12, are assumed to lie within the 100-year period centered about the current year.

C = datevec(A,P) uses the specified pivot year as the starting year of the 100-year range in which a two-character year resides. The default pivot year is the current year minus 50 years.

[Y,M,D,H,MI,S] = datevec(A) returns the components of the date vector as individual variables.

When creating your own date vector, you need not make the components integers. Any components that lie outside their conventional ranges affect the next higher component (so that, for instance, the anomalous June 31 becomes July 1). A zeroth month, with zero days, is allowed.

Examples

An example of using a string as input:

  • datevec('12/24/1984')

    ans =
    1984 12 24 0 0 0

An example of using a serial date number as input:

  • t = datenum('12/24/1984')

    t =
    725000

    datevec(t)

    ans =
    1984 12 24 0 0 0





No comments:

Post a Comment