File Exchange

image thumbnail

Round Serial Date Numbers or Date Vectors

version 1.5 (2.85 KB) by

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

7 Downloads

Updated

View License

This function rounds Serial Date Numbers or Date Vectors to the nearest year, month, day, hour, minute, or second. With a choice of rounding, floor or ceiling.

### 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]

Comments and Ratings (3)

Jeff Jackson

Jeff Jackson (view profile)

Jeff Jackson

Jeff Jackson (view profile)

Jeff H

Jeff H (view profile)

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

1.5

* Make string arguments case insensitive.
* Improve input argument checking.

1.4

- Revert conversion precision adjustment to original value.

1.3

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

1.2

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

1.1

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

MATLAB Release
MATLAB 7.11 (R2010b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video