separate date from time

1 次查看(过去 30 天)
hi,
i have timestamp in my data
such as:
874965478
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?

采纳的回答

per isakson
per isakson 2012-7-26
编辑:per isakson 2012-7-26
The function, floor, does it:
>> floor( 874965478/86400 + datenum('1/1/1970') )
ans =
729655
and check the result
>> datestr( ans, 31 )
ans =
1997-09-22 00:00:00
>>
This, 729655, is serial date number.

更多回答(1 个)

Star Strider
Star Strider 2012-7-26
Alternatively, change:
d=datestr(874965478/86400 + datenum('1/1/1970'))
to:
d=datevec(874965478/86400 + datenum('1/1/1970'))
then:
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.
  4 个评论
huda nawaf
huda nawaf 2012-7-29
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
thanks
per isakson
per isakson 2012-7-29
编辑:per isakson 2012-7-29
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by