在 R 中从 UTC 转换为日期格式 [英] Converting from UTC into date format in R

查看:39
本文介绍了在 R 中从 UTC 转换为日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UTC 时间戳.我想在 R 中将其转换为 YYYY/MM/DD 格式.

I have a UTC timestamp. I want to convert it into YYYY/MM/DD format in R.

例如,1318394558766.我尝试了 format 命令失败.

For example, 1318394558766. I tried format command unsuccessfully.

感谢任何帮助.

谢谢!

推荐答案

您可以使用 as.POSIXctas.POSIXlt.但是,您必须知道毫秒数的起始日期.

You can use as.POSIXct or as.POSIXlt. However you have to know the origin date from which your number of milliseconds started.

as.POSIXct(1318394558766/1000, origin='1970-01-01')

> unlist(as.POSIXlt(1318394558766/1000, origin='1970-01-01'))
    sec     min    hour    mday     mon    year    wday    yday   isdst 
 38.766  42.000  21.000  11.000   9.000 111.000   2.000 283.000   1.000 
> 

然后就可以使用format得到想要的YYYY/MM/DD:

Then you can use format to get the desired YYYY/MM/DD:

format(as.POSIXct(1318394558766/1000, origin='1970-01-01'), format='%Y/%m/%d')

这篇关于在 R 中从 UTC 转换为日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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