[原創]Matlab 獲取當前時間信息


本文主要介紹下Matlab中如何獲取當前時間的一些方法。

基本變量 date、now、clock

  1. date 按照日期字符串返回當前系統時間
  2. now 按照連續的日期數值返回當前系統時間
  3. clock 按照日期向量格式返回當前系統時間
>> date, now, clock

ans =

15-May-2016


ans =

   7.3647e+05


ans =

   1.0e+03 *

    2.0160    0.0050    0.0150    0.0170    0.0100    0.0195

使用 year、month、day 等函數獲取

可以使用的獲取函數有 year, month, day, hour, minute, second,其作用是從第一部分講到的字符串和連續型日期時間個時鍾提取信息。

>> [year(date), year(now)]

ans =

        2016        2016

但是,其沒有辦法從向量型日期中讀取信息。

>> year(clock)

ans =

     5     0     0     0     0     0

其他函數 datestr 等

還有一些日期轉字符串的函數,比如datestr。

>> datestr(date), datestr(now), datestr(clock)

ans =

15-May-2016


ans =

15-May-2016 17:20:03


ans =

15-May-2016 17:20:03

有關於 datestr 函數和其他相關類似的函數,可以通過 help datestr 或者 doc datestr 進行訪問查看。以下為摘取部分(有刪減)

>> help datestr
 datestr String representation of date.
 
    S = datestr(D, F) converts one or more date vectors, serial date
    numbers, or date strings D into the same number of date strings S.
    Input argument F is a format number or string that determines the
    format of the date string output. Valid values for F are given in Table
    1, below. Input F may also contain a free-form date format string
    consisting of format tokens as shown in Table 2, below. 
 
 	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 
 
    Examples:
 	datestr(now) returns '24-Jan-2003 11:58:15' for that particular date,
 	on an US English locale datestr(now,2) returns 01/24/03, the same as
 	for datestr(now,'mm/dd/yy') datestr(now,'dd.mm.yyyy') returns
 	24.01.2003 To convert a non-standard date form into a standard MATLAB
 	dateform, first convert the non-standard date form to a date number,
 	using DATENUM, for example, 
 	datestr(DATENUM('24.01.2003','dd.mm.yyyy'),2) returns 01/24/03.
 
 	See also date, datenum, datevec, datetick.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM