MATLAB Answers

huda nawaf

separate date from time

Asked by huda nawaf
on 26 Jul 2012


i have timestamp in my data

such as:


which contains date and time

what I need is separate date from time

I try this

d=datestr(874965478/86400 + datenum('1/1/1970'))

and get

d =

22-Sep-1997 21:57:58

this result do not what i need, i want the timestamp remain integer but without time just date

can do that?




No products are associated with this question.

2 Answers

Answer by per isakson
on 26 Jul 2012
Edited by per isakson
on 26 Jul 2012
 Accepted answer

The function, floor, does it:

    >> floor( 874965478/86400 + datenum('1/1/1970') )
    ans =

and check the result

    >> datestr( ans, 31 )
    ans =
    1997-09-22 00:00:00

This, 729655, is serial date number.

  1 Comment

many thanks

Answer by Star Strider
on 26 Jul 2012

Alternatively, change:

d=datestr(874965478/86400 + datenum('1/1/1970'))


d=datevec(874965478/86400 + datenum('1/1/1970'))


d_date = d(1:3)

This gives you the date as a [YYYY MM DD] vector. You can format it as you like as a string with ‘sprintf’ or other functions.


many thanks

hi again,

why when I used this timestamp: 9783021091

and use d=datestr(9783021091/86400 + datenum('1/1/1970')); get 05-Jan-2280 09:51:31

while when use d=datestr(874965478/86400 + datenum('1/1/1970'))

get d =

22-Sep-1997 21:57:58


You write: "Why", but doesn't say why you question the result.

Your timestamps are seconds after 1/1/1970(?)

In the second case the integer, 874965478, corresponds to approx. 27 years. In the first case the integer, 9783021091, is eleven times larger, i.e. 300 years. The results is plausible.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!