1970 matla 时间_如何在Matlab中使用Unix时间戳?

I have some data files with Unix timestamps (in this case, number of milliseconds since Jan 1, 1970 00:00 UTC). I would like to convert these to human-friendly date/time strings (e.g. 31-Aug-2012 11:36:24) in Matlab. Is there an easy way to do this in Matlab, or am I better off using an external library (e.g. java.text.SimpleDateFormat)?

解决方案

How about

date = datestr(unix_time/86400 + datenum(1970,1,1))

if unix_time is given in seconds, unix_time/86400 will give the number of days since Jan. 1st 1970. Add to that the offset used by Matlab's datenum (datenum(0000,1,1) == 1), and you have the amount of days since Jan. 1st, 0000. This can be easily converted to human-readable form by Matlab's datestr.

If you have milliseconds, just use

date = datestr(unix_time/86400/1000 + datenum(1970,1,1))

Wrapped in functions, these would be

function dn = unixtime_to_datenum( unix_time )

dn = unix_time/86400 + 719529; %# == datenum(1970,1,1)

end

function dn = unixtime_in_ms_to_datenum( unix_time_ms )

dn = unix_time_ms/86400000 + 719529; %# == datenum(1970,1,1)

end

datestr( unixtime_to_datenum( unix_time ) )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值