剥离日期并保持时间 [英] Strip the date and keep the time

查看:170
本文介绍了剥离日期并保持时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多人问如何剥夺时间和保持日期,但是呢呢呢?给定:

  myDateTime<  - 11/02/2014 14:22:45

我想看看:

  myTime 
[1]14:22:45

时区不是必需的。



我已经尝试了(从其他答案)

  as .POSIXct(substr(myDateTime,12,19),format =%H:%M:%S)

[1]2013-04-13 14:22:45 NZST



目的是分析几天内按时间记录的事件



感谢



编辑



原来没有纯粹的时间对象,所以每次也必须有一个日期。



最后我使用了

  as.POSIXct(as.numeric(as.POSIXct(myDateTime))%% 86400,origin =2000-01-01)

而不是字符解决方案,因为我需要对结果进行算术。这个解决方案类似于我的原来的一个,除了日期可以一致地被控制 - 2000-01-01在这种情况下,而我的尝试只是使用当前日期在运行时。

解决方案

如果GMT日期内的时间对您的问题有用,可以使用 %% 余数运算符,取余数mod $ 86400 (一天中的秒数)。

 code>邮票<  -  c(2013-04-12 19:00:00,2010-04-01 19:00:01,2018-06-18 19:00:02) 
as.numeric(as.POSIXct(stamps))%% 86400
## [1] 0 1 2


Lots of people ask how to strip the time and keep the date, but what about the other way around? Given:

myDateTime <- "11/02/2014 14:22:45"

I would like to see:

myTime
[1] "14:22:45"

Time zone not necessary.

I've already tried (from other answers)

as.POSIXct(substr(myDateTime, 12,19),format="%H:%M:%S")

[1] "2013-04-13 14:22:45 NZST"

The purpose is to analyse events recorded over several days by time of day only.

Thanks

Edit:

It turns out there's no pure "time" object, so every time must also have a date.

In the end I used

as.POSIXct(as.numeric(as.POSIXct(myDateTime)) %% 86400, origin = "2000-01-01")

rather than the character solution, because I need to do arithmetic on the results. This solution is similar to my original one, except that the date can be controlled consistently - "2000-01-01" in this case, whereas my attempt just used the current date at runtime.

解决方案

If the time within a GMT day is useful for your problem, you can get this with %%, the remainder operator, taking the remainder modulo 86400 (the number of seconds in a day).

stamps <- c("2013-04-12 19:00:00", "2010-04-01 19:00:01", "2018-06-18 19:00:02")
as.numeric(as.POSIXct(stamps)) %% 86400
## [1] 0 1 2

这篇关于剥离日期并保持时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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