【问题标题】:How to a convert a date to a number and back again in MATLAB如何在 MATLAB 中将日期转换为数字并再次返回
【发布时间】:2011-04-26 15:15:09
【问题描述】:

我有日期 2010 年 1 月 11 日

并使用函数

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

使用 Excel 将日期转换为 40189 的数字。

我可以使用 MATLAB 将数字 40189 再次转换回日期吗?

【问题讨论】:

    标签: excel matlab date


    【解决方案1】:

    使用DATESTR

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

    很遗憾,Excel 从 1900 年 1 月 1 日开始计数。了解如何使用 DATENUM 将序列日期从 Matlab 转换为 Excel

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

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

    datestr(excelSerialDate + 693960)
    

    编辑

    要获取 mm/dd/yyyy 格式的日期,请使用指定格式调用datestr

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

    另外,如果你想去掉本月的前导零,你可以使用REGEXPREP 来解决问题

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

    【讨论】:

    • 请注意,有些 Excel 文件的纪元从 1904 年开始,而不是 1900 年。(请参阅support.microsoft.com/kb/180162)如果您正在编写通用 Excel/Matlab 日期转换代码,则需要测试或提供输入以指示 1904 特殊情况。 1904 纪元 Excel 文件的日期偏移量为 695422。
    猜你喜欢
    • 2012-11-01
    • 2013-10-29
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2013-02-18
    • 2011-01-13
    • 2011-06-23
    相关资源
    最近更新 更多