如何在MATLAB中将日期转换为数字,然后再次 [英] How to a convert a date to a number and back again in MATLAB

查看:948
本文介绍了如何在MATLAB中将日期转换为数字,然后再次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有日期
1/11/2010

I have the date 1/11/2010

并使用函数

=日期(年(A1),月(A1),日(A1))

= date(year(A1),month(A1),day(A1))

将日期转换为40189与Excel。

to convert the date to the number to 40189 with Excel.

我可以使用MATLAB将号码40189重新转回日期吗?

Can I use MATLAB to convert the number 40189 back to the date again?

推荐答案

使用 DATESTR

>> datestr(40189)
ans =
12-Jan-0110

不幸的是, 1900年1月1日开始计数。了解如何使用 DATENUM 将串行日期从Matlab转换为Excel / p>

Unfortunately, Excel starts counting at 1-Jan-1900. Find out how to convert serial dates from Matlab to Excel by using DATENUM

>> datenum(2010,1,11)
ans =
      734149
>> datenum(2010,1,11)-40189
ans =
      693960
>> datestr(40189+693960)
ans =
11-Jan-2010

换句话说,要转换任何串行Excel日期,请调用

In other words, to convert any serial Excel date, call

datestr(excelSerialDate + 693960)

编辑

/ dd / yyyy格式,调用 datestr 指定格式

To get the date in mm/dd/yyyy format, call datestr with the specified format

excelSerialDate = 40189;
datestr(excelSerialDate + 693960,'mm/dd/yyyy')
ans =
01/11/2010

另外,如果你想摆脱这个月的前导零,你可以使用 REGEXPREP 来修复问题

Also, if you want to get rid of the leading zero for the month, you can use REGEXPREP to fix things

excelSerialDate = 40189;
regexprep(datestr(excelSerialDate + 693960,'mm/dd/yyyy'),'^0','')
ans =
1/11/2010

这篇关于如何在MATLAB中将日期转换为数字,然后再次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆