Code covered by the BSD License  

Highlights from
Round Serial Date Numbers or Date Vectors

5.0

5.0 | 3 ratings Rate this file 20 Downloads (last 30 days) File Size: 2.79 KB File ID: #39274

Round Serial Date Numbers or Date Vectors

by

 

03 Dec 2012 (Updated )

Round Serial Date Numbers or Date Vectors to the nearest desired unit (round/floor/ceiling).

| Watch this File

File Information
Description

### Why This Function? ###

# Round dates to the nearest year, month, day, hour, minute, or second.
# Choice of rounding, floor or ceiling.
# Input either Serial Date Numbers in a column vector, or Date Vectors in a matrix.
# Output is a column vector of Serial Date Numbers.

### Examples ###

Examples use the date+time described by the vector [1999,1,3,15,6,48.0568].

datevec(dateround(730123.62972287962))
 ans = [1999,1,3,15,6,48]

datevec(dateround([1999,1,3,15,6,48.0568]))
 ans = [1999,1,3,15,6,48]

datevec(dateround([1999,1,3,15,6,48.0568],'minute'))
 ans = [1999,1,3,15,7,0]

datevec(dateround([1999,1,3,15,6,48.0568],5)) % 5=='minute'
 ans = [1999,1,3,15,7,0]

datevec(dateround([1999,1,3,15,6,48.0568],5,'floor'))
 ans = [1999,1,3,15,6,0]

datevec(dateround([1999,12,31,23,59,59.5000;1999,12,31,23,59,59.4999]))
 ans = [2000,1,1,0,0,0;1999,12,31,23,59,59]

Acknowledgements

This file inspired Iso 8601 Date String To Serial Date Number and Date Vector/Number To Iso 8601 Date String.

Required Products MATLAB
MATLAB release MATLAB 7.11 (R2010b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (3)
14 Aug 2014 Jeff Jackson  
14 Aug 2014 Jeff Jackson  
16 Apr 2013 Jeff Harris

Nicely done. It is useful to have a time rounding fn to determine if times are equal within a tolerance, especially if they may have lost significant digits.

Updates
10 Dec 2012

- Fix bug in 'ceiling'.
- Unit tokens changed to match datestr8601/datenum8601 tokens.
- Simplify date format selection code.

21 May 2013

- Now only returns Date Numbers. This simplifies the code and the help comments.

24 Jun 2013

- Fixed incorrect order when rounding multiple dates.
- Improve internal precision management.

08 Jul 2013

- Revert conversion precision adjustment to original value.

Contact us