1.now 返回當前的日期值(這個數的整數部分表示從公元0年1月1日到該日的天數,小數部分則表示具體的時刻)
eg.
t=now
t =7.3354e+005
2.datestr 日期轉換成字元形式
調用格式 datestr(date,dateform) date為要轉換的日期值,dateform為日期格式參數(具體參數值可通過 help datestr查看協助)
dateform參數說明
Table 1: Standard MATLAB Date format definitions
Number 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-96
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-1996
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
Table 2: Free-form date format symbols
Symbol Interpretation of format symbol
===========================================================================
yyyy full year, e.g. 1990, 2000, 2002
yy partial year, e.g. 90, 00, 02
mmmm full name of the month, according to the calendar locale, e.g.
"March", "April" in the UK and USA English locales.
mmm first three letters of the month, according to the calendar
locale, e.g. "Mar", "Apr" in the UK and USA English locales.
mm numeric month of year, padded with leading zeros, e.g. ../03/..
or ../12/..
m capitalized first letter of the month, according to the
calendar locale; for backwards compatibility.
dddd full name of the weekday, according to the calendar locale, e.g.
"Monday", "Tuesday", for the UK and USA calendar locales.
ddd first three letters of the weekday, according to the calendar
locale, e.g. "Mon", "Tue", for the UK and USA calendar locales.
dd numeric day of the month, padded with leading zeros, e.g.
05/../.. or 20/../..
d capitalized first letter of the weekday; for backwards
compatibility
HH hour of the day, according to the time format. In case the time
format AM | PM is set, HH does not pad with leading zeros. In
case AM | PM is not set, display the hour of the day, padded
with leading zeros. e.g 10:20 PM, which is equivalent to 22:20;
9:00 AM, which is equivalent to 09:00.
MM minutes of the hour, padded with leading zeros, e.g. 10:15,
10:05, 10:05 AM.
SS second of the minute, padded with leading zeros, e.g. 10:15:30,
10:05:30, 10:05:30 AM.
FFF milliseconds field, padded with leading zeros, e.g.
10:15:30.015.
PM set the time format as time of morning or time of afternoon. AM
or PM is appended to the date string, as appropriate.
eg.
>> t=now;datestr(t,0)
ans =09-May-2008 21:07:23
3.date將字串轉換成日期值
調用文法:date(year,month,day)
datenum(year,month,day,hour,minute,second)
eg.
>> t=now;datestr(t,0)
ans =09-May-2008 21:07:23
>> datenum(ans)
ans =7.3354e+005
>>
4.datevec將datestr中指定的格式日期字元轉換成包含日期分量的數值向量
eg.
>>c=datevec('09-May-2008 21:07:23')
c = 2008 5 9 21 7 23
5.weekday 可從一個日期值或字串中找出具體的日子和星期。
eg.
>> [d,w]=weekday( 7.3354e+005)d = 2
w =Mon>>[d,w]=weekday('21-Dec-2008')d = 1
w =Sun6.eomday 返回任何一個月最後一天是幾號
調用文法:eomday(year,month) 因為閏年的存在故需要year這個參數。
eg.
>> eomday(2008,2)
ans = 29
>> eomday(2007,2)
ans = 28
7.calendar 產生指定月份的日曆。
eg
>> calendar(date)
May 2008
S M Tu W Th F S
0 0 0 0 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
0 0 0 0 0 0 0
>> calendar(2008,5)
May 2008
S M Tu W Th F S
0 0 0 0 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
0 0 0 0 0 0 0>>
補充:date返回當天日期的字串
eg.
>> dateans =09-May-20088.tic ,toc 兩個結合使用可計算一組matlab操作指令的執行所需時間
eg.
>> tic; plot(rand(500,5));toc
Elapsed time is 0.078000 seconds.
另外Matlab還提供了cputime和etime(具體使用方法可看協助文檔),用來計算一次運算所佔cpu時間.
eg.
>>t0=cputime;myoperation;cputime-t0
ans=0.1400
>>t1=clock;myoperation;etime(clock,t1)
ans=11.2800
註:myoperation為使用者自訂指令檔
>> t0=cputimet0 = 53.8125
>> t1=cputimet1 = 54.0313
>> deltaT=t1-t0
deltaT = 0.2188
>>
t1=clockt1 =1.0e+003 * 2.0080 0.0050 0.0090 0.0210
0.0460 0.0254>> deltaT=etime(t1,t0)deltaT = 9.9060>>
9.datetick 設一個座標為時間標籤eg.
>> t=(1900:10:1990)';
>> p=[100 200 150 350 200 400 500 100 30 120];
>> plot(datenum(t,1,1),p);
>> datetick('x','yyyy')
>> title('test time tip')