2
votes

Let

x=7.369030000162731e+05

x is a matlab date and it is equal to

27.07.2017 00:00:01.406

I want to remove the milliseconds from it (ie. .406)

To do this I convert it to datestr with 'dd.mm.yyyy HH:MM:SS' format and then again to datenum

datenum(datestr(x,'dd.mm.yyyy HH:MM:SS'))

Is there a simpler way to do this.

2
For what it is worth, you can't accurately compare your approach with the answers below because of a subtle bug here (at least for my locale). Your date format string is different from what datenum is expecting and the result is changed to from July 27, 2017 to January 1, 2017 (for me). One way to fix this is to explicitly pass the format string as a second parameter to datenum like this: datenum(datestr(x,'dd.mm.yyyy HH:MM:SS'),'dd.mm.yyyy HH:MM:SS')).informaton

2 Answers

5
votes

Here's a somewhat simpler way that converts x to a date vector, floors all the elements (which only affects the seconds value in index 6), then converts it back to a serial date number:

x = datenum(floor(datevec(x)));
5
votes

If you want the manual approach:

y = floor(x*86400)/86400;

because serial date numbers are measured in days, and 86400 is the number of seconds in a day.