简体   繁体   English

r日期转换专用格式

[英]r Date conversion specific format

I have a date in POSIXlt format: 我有一个POSIXlt格式的日期:

mydate

[1] "2011-12-25 15:47:21" [1]“ 2011-12-25 15:47:21”

and I want a character string "12/25/2011" 我想要一个字符串“ 12/25/2011”

I have tried using "mydate$mon+1/mydate$mday/mydate$year+1900" but I have problems making R evaluate the right terms. 我尝试使用"mydate$mon+1/mydate$mday/mydate$year+1900"但是在使R评估正确的条件时遇到问题。

Also, I tried as.Date with format="%m/%d/%y" but it ignored that format and output 另外,我尝试了as.Dateformat="%m/%d/%y"但它忽略了该格式并输出了

as.Date(mydate)

[1] "2011-12-25" [1]“ 2011-12-25”

Thanks, 谢谢,

Hi are you intending to achieve this? 嗨,您打算实现这一目标吗?

 new <- as.POSIXlt("2011-12-25 15:47:21")
 format(new, format="%d/%m/%Y")

I am going to put in a shameless self-plug for an R package I am developing, which will do this automatically. 我将为正在开发的R封装插入一个无耻的自插入插头,它将自动执行此操作。 Version 0.0.1 is available here https://github.com/ramnathv/intellidate 可在此处获得版本0.0.1 https://github.com/ramnathv/intellidate

library(intellidate)
new <- as.POSIXlt("2011-12-25 15:47:21")
str_to_date(new)

"2011-12-25"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM