【问题标题】:18 digit timestamp to regular date format18 位时间戳到常规日期格式
【发布时间】:2021-10-13 22:52:34
【问题描述】:

我有一个带有 18 位时间戳的数据集。

如何将它们转换为常规日期格式,例如“%Y-%m-%d %H:%M:%0S6”?

复制一个时间戳是:637201135513320448


我尝试使用as.POSIXct() 提到的here 函数,但它返回NA。我认为它不是为这么多数字而设计的。我在 c#here 上找到了一个提示,其中有人遇到了类似的问题。但我无法在 R 上解决这个问题。

【问题讨论】:

  • 获取时间戳 0 (as.POSIXct) 到某个日期,找到一个在线工具来提供该日期的 .NET 时间戳。现在只需删除这个常量的所有时间戳,你就会得到 POSIXct 次

标签: r datetime


【解决方案1】:

根据 Julian_Hn(credits) 的指导,这里是一个使用 lubridate 的函数:

library(lubridate)

my_18digit_converter <- function(x){
  y <- (x - 621355968000000000) / 1e7
  lubridate::as_datetime(y)
}

my_18digit_converter(x)

输出:

> my_18digit_converter(x)
[1] "2020-03-18 07:32:31 UTC"

【讨论】:

    【解决方案2】:

    如果这确实是 C# 类型的时间戳,您首先需要将其转换为正确的值,as.POSIXct 才能工作:

    timestamp <- 637201135513320448
    epoch0.Csharp <- 621355968000000000 # value of Unix Time Origin in C# timestamp
    
    timestamp.conv <- (timestamp - epoch0.Csharp) / 1e7 # C# has 10000 ticks per millisecond == 1e7 ticks per second which is the unit of the unix timestamp
    
    dateTime <- as.POSIXct(timestamp.conv,origin="1970-01-01")
    
    dateTime 
    [1] "2020-03-18 08:32:31 CET"
    

    【讨论】:

      猜你喜欢
      • 2017-06-08
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      • 2018-06-15
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 2016-02-13
      相关资源
      最近更新 更多