【问题标题】:converting Excel-date string to date and time将 Excel 日期字符串转换为日期和时间
【发布时间】:2014-04-23 18:59:44
【问题描述】:

我从 Excel 中导入了一些包含日期和时间单元格的数据。当我尝试将数据转换为日期和时间格式时,我只会得到非常奇怪的结果。如果我只约会没有问题,我会得到正确的日期。 (我在 MacOS 上工作)

date <- 40259.93645833333

as.POSIXlt(date,origin='1904-01-01 00:00:00')
"1904-01-01 12:10:59 CET"
as.POSIXlt(date,origin='1970-01-01 00:00:00')
"1970-01-01 12:10:59 CET"
as.POSIXlt(date,origin='2010-01-01 00:00:00')
2010-01-01 12:10:59 CET"
as.Date(date,origin='1904-01-01')
"2014-03-23" #Correct date

我想知道如何继续并保持时间。

【问题讨论】:

  • 你的excel的来源是什么?第一年是哪一年?
  • 指定的日期是我的 Excel 文件中的第一个日期。
  • 您尝试过 XLConnect 吗?它应该正确地导入数据(包括日期)。
  • 我使用 xlsx 导入了我的数据。

标签: r excel date datetime


【解决方案1】:

传递给as.POSIXlt 的数字是自原始以来的秒数:

as.POSIXlt(date*24*3600,origin='1904-01-01 00:00:00')
[1] "2014-03-23 23:28:29 CET"

此外,一般建议建议使用 POSIXct 而不是 POSIXlt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 2021-07-14
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    相关资源
    最近更新 更多