Code covered by the BSD License  

Highlights from
DATEPART

3.0

3.0 | 2 ratings Rate this file 2 Downloads (last 30 days) File Size: 2.08 KB File ID: #15585

DATEPART

by Ian Howat

 

14 Jul 2007 (Updated 06 Aug 2007)

Extract decimal yr/month/day/hr/min/sec from a datenum. More versatile than datevec.

| Watch this File

File Information
Description

X = DATEPART(DATE,'part') returns the date 'part' (year, month,day, hour, minute or second) of the datenum date with the decimal equivalent of the trailing date parts in the units of 'part'. DATE can be scalar, vector or array and X will be of the some size and shape.

  [X1,X2,...] = DATEPART(DATE,'part1','part2',...) returns each decimal value corresponding to each part input argument.

  Acceptable string specifiers for 'part' are:
  Year: 'year','yr','years','yrs'
  Month: 'month','mn','months','mns'
  Day: 'day','days'
  Hour: 'hour','hours','hr','hrs'
  Minute: 'minutes','minutes','min','mins'
  Second: 'second','seconds','sec','secs'

  Examples:
  >> A = datenum('14-Jul-2007 15:05:12')
  >> datepart(A,'min')
  ans =
       5.2000
  >> floor(datepart(A,'min'))
  ans =
      5
  >> format bank
  >> datepart(A,'year')
  ans =
      2007.53
  >> [day,yr] = datepart(A,'day','year')
  day =
         14.63
  yr =
       2007.53

MATLAB release MATLAB 7.2 (R2006a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
06 Aug 2008 Cory Sieg

Doesn't pick out the month accurately from a daily time series.

13 Aug 2008 Ian Howat

Cory,
Please give some more details of your error *BEFORE* trashing the function with a 1-star rating so that I can fix it. You say a that the function is failing in returning the correct month with a 1-day time series. If I create such as time series with:
>t = datenum('jan-0-2008')+(1:365);
and then sample this time series about once a month:
>> ts = t(1:30:end);
>> datestr(ts)
ans =
01-Jan-2008
31-Jan-2008
01-Mar-2008
31-Mar-2008
30-Apr-2008
30-May-2008
29-Jun-2008
29-Jul-2008
28-Aug-2008
27-Sep-2008
27-Oct-2008
26-Nov-2008
26-Dec-2008

and then get the decimal month using datepart:
>> datepart(ts,'month')
ans =
  Columns 1 through 9
    1.0000 1.9677 3.0000 3.9677 4.9667 5.9355 6.9333 7.9032 8.8710
  Columns 10 through 13
    9.8667 10.8387 11.8333 12.8065
Looks good to me ... sure you're using it correctly??????

Please login to add a comment or rating.
Updates
19 Jul 2007

Corrected summary

19 Jul 2007

Added multi-argument capability.

23 Jul 2007

Corrected summary, again.

06 Aug 2007

Fixed error in seconds reporting.

Tag Activity for this File
Tag Applied By Date/Time
clock Ian Howat 22 Oct 2008 09:19:21
fun Ian Howat 22 Oct 2008 09:19:21
date Ian Howat 22 Oct 2008 09:19:21
time Ian Howat 22 Oct 2008 09:19:21
datestr Ian Howat 22 Oct 2008 09:19:21
datenum Ian Howat 22 Oct 2008 09:19:21

Contact us at files@mathworks.com