一、日期和時間
1.生成指定格式日期和時間
標准日期格式
2.獲取當前時間的數值
>> datestr(now,31) ans =
2017-09-11 19:56:26
>> datestr(now,'yyyy-mm-dd HH:MM:SS') ans =
2017-09-11 19:58:28
>> clock ans =
1.0e+03 *
2.0170 0.0090 0.0110 0.0200 0.0030 0.0264
>> datestr(now,'HH:MM:SS') ans =
20:05:07
>> format short g >> clock ans =
2017 9 11 20 5 42.98
>> now=fix(clock) now =
2017 9 11 20 9 16
上述轉化為向量形式,可以隨意調用年月日時分秒
向量和天數轉化
3.其他時間和日期函數或命令
>> date ans =
11-Sep-2017
從公元0年到現在的天數
>> n=datenum(now) n =
7.3695e+05
>> fix(n) ans =
736949
>> today ans =
736949
clear all DateString = '19-May-2001'; formatIn = 'dd-mmm-yyyy'; datenum(DateString,formatIn)
向量向字符串轉變
兩次時間差(測試函數運行時間的函數)
>> t0=clock t0 =
2017 9 11 20 27 56.81
>> etime(clock,t0) ans =
18.8
>> t1=clock t1 =
2017 9 11 20 28 33.745
>> etime(t1,t0) ans =
36.935
利用占用CPU時間差,來進行時間差計算
>> t=cputime; >> T=cputime-t T =
0.3276
tic/toc
>> tic%開始計時 >> toc%結束計時 時間已過 1.945333 秒。
星期值
>> weekday(today) ans =
2
>> [a,b]=weekday(today) a =
2 b = Mon >> [a,b]=weekday(today,'long') a =
2 b = Monday
某月的天數
eomday(2017,9) ans =
30
某年的天數
>> yeardays(2017) ans =
365
月份表
>> calendar Sep 2017 S M Tu W Th F S 0 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 0 0 0 0 0 0 0
>> calendar(2017,10) Oct 2017 S M Tu W Th F S 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 0 0 0 0
4、時間坐標轉換函數 datetick函數
clear all t = (1900:10:1990)';
p = [75.995 91.972 105.711 123.203 131.669 ... 150.697 179.323 203.212 226.505 249.633]';
figure(1) subplot(211),bar(t,p) subplot(212),bar(datenum(t,1,1),p)
clear all t = (1900:10:1990)';
p = [75.995 91.972 105.711 123.203 131.669 ... 150.697 179.323 203.212 226.505 249.633]';
figure(1) subplot(211),bar(t,p) subplot(212),bar(datenum(t,1,1),p) dateFormat = 11; %'yy'形式 datetick('x',dateFormat); %將x坐標的值換為,yy形式
figure subplot(211),plot(n,t); subplot(212),plot(n,t); datetick('x','HH:MM'); grid on