【问题标题】:Formatting String to a Date with Hours and Minutes将字符串格式化为带小时和分钟的日期
【发布时间】:2019-10-18 14:05:19
【问题描述】:

我得到了一个如下所示的时间字符串: 201902041502, containing year, month, day, hour and minute。 现在我想将此字符串重新格式化为德语日期时间格式,如下所示:04.02.2019 15:02.

我已经尝试过as.Dateas.POSIXct,但它不起作用,我想避免增加几秒钟让POSIXct 工作。

提前致谢!干杯

【问题讨论】:

  • 你试过了吗? strftime(as.POSIXct('201902041502',format='%Y%m%d%H%M'), format='%d.%m.%Y %H:%M')

标签: r date dplyr


【解决方案1】:

您可以使用strptime将数据转换为POSIXlt对象,

x <- "201902041502"
xd <- strptime(x,"%Y%m%d%H%M")
# [1] "2019-02-04 15:02:00 CET"

然后使用strftime 生成您想要的格式:

strftime(xd, "%d.%m.%Y %H:%M")
# [1] "04.02.2019 15:02"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-06
    • 2021-06-22
    • 2018-09-01
    • 2013-02-13
    • 2012-08-29
    • 1970-01-01
    • 2012-09-07
    相关资源
    最近更新 更多