如何从字符串转换日期? [英] How to convert a date from a character string?

查看:52
本文介绍了如何从字符串转换日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索过,但无法找出如何从格式如下的字符串转换日期:

I have searched but I could not find out how to convert a date from a character string formatted as follows:

date <- "07-21-2015-09:30AM"

我想使用 as.Date ,但我没有做到。我得到的是以下内容:

I wanted to use as.Date, but I have not manage to. All I get is the following:

as.Date(date, format="%m-%d-%y-%hAM")
NA

as.Date(dates, format="%m-%d-%y-%h")
NA


推荐答案

如果我们需要'date'和'time',则一个选项是 as.POSIXct

If we need the 'date' and 'time', one option is as.POSIXct

 as.POSIXct(date, format='%m-%d-%Y-%I:%M%p')
 #[1] "2015-07-21 09:30:00 EDT"

这篇关于如何从字符串转换日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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