如何从strptime中提取年份? [英] How can I extract year from strptime?

查看:147
本文介绍了如何从strptime中提取年份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的格式:

  t < - 4/2/2004 12:45

我可以使用这两行来提取日期和月份:

strptime(t,%m /%d /%Y%H:%M,tz =UTC )$ day
strptime(t,%m /%d /%Y%H:%M,tz =UTC)$ mday

但是,这并不是我想要的岁月。我给了我这个世纪的岁月:

  strptime(t,%m /%d /%Y%H:%M ,tz =UTC)$ year 

像1987年,2012年等等,而不是87和12。



如果还有其他功能用于此,我也可以这样做。

解决方案

添加1900.请参阅?POSIXlt 详细信息部分> for,um,details。

 > tm<  - 4/2/2004 12:45
> strptime(tm,%m /%d /%Y%H:%M,tz =UTC)$ year
[1] 104
> strptime(tm,%m /%d /%Y%H:%M,tz =UTC)$ year + 1900
[1] 2004
/ pre>

I have times in this format:

t <- "4/2/2004 12:45"

I can extract day and month using these two lines:

strptime(t, "%m/%d/%Y %H:%M", tz="UTC")$day
strptime(t, "%m/%d/%Y %H:%M", tz="UTC")$mday

But this doesn't give me the years I want. I gives me year without the century:

strptime(t, "%m/%d/%Y %H:%M", tz="UTC")$year

How can I get the year exactly? Like 1987, 2012 etc. And not 87 and 12.

If there is any other function to use for this, I am OK with that as well.

解决方案

Add 1900. See the Details section of ?POSIXlt for, um, details.

> tm <- "4/2/2004 12:45"
> strptime(tm, "%m/%d/%Y %H:%M", tz="UTC")$year
[1] 104
> strptime(tm, "%m/%d/%Y %H:%M", tz="UTC")$year + 1900
[1] 2004

这篇关于如何从strptime中提取年份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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