如何优雅地将日期时间从十进制转换为“%d。%m。%y%H:%M:%S”?

时间:2022-05-23 21:26:34

I am doing an experiment which produces automatically logged data. The software produces a timestamp that is of the format 41149.014850. I would like to convert this decimal timestamp to 28.08.2012 00:21:23. How can I do this in R most elegantly?

我正在做一个产生自动记录数据的实验。该软件生成时间戳,格式为41149.014850。我想将此十进制时间戳转换为28.08.2012 00:21:23。我怎样才能最优雅地在R中做到这一点?

I tried using the function strsplit and also the function as.Date with a specified origin, and also the times function. But to no avail. I have problems in splitting the timestamp into two numbers that I can access with the functions as.Date and times.

我尝试使用函数strsplit以及具有指定原点的函数as.Date,以及times函数。但无济于事。我在将时间戳分成两个数字时遇到问题,我可以使用函数as.Date和times访问这两个数字。

Here is some demo code:

这是一些演示代码:

myDatetime <- c(41149.004641, # 28.08.2012  00:06:41
41149.009745, # 28.08.2012  00:14:02
41149.014850, # 28.08.2012  00:21:23
41149.019954) # 28.08.2012  00:28:44

## not working out for me
Dat.char <- as.character(myDatetime)
date.split <- strsplit(Dat.char, split = "\\.")
## how to proceed from here, if it is a good way at all

Please let me know if you need more information about the challenge or if I can pose the question in a better way. Any searching was to no avail.

如果您需要有关挑战的更多信息,或者我是否可以更好地提出问题,请告诉我。任何搜索都无济于事。

Direct help or linkage to other websites/posts is greatly appreciated.

非常感谢直接帮助或与其他网站/帖子的链接。

2 个解决方案

#1


10  

You dates are in an Excel-like date format (days after January 1, 1900), so you need to convert them to an R date format. Then you can convert it to a datetime format (POSIXct).

您的日期采用类似Excel的日期格式(1900年1月1日之后的日期),因此您需要将它们转换为R日期格式。然后,您可以将其转换为日期时间格式(POSIXct)。

# first convert to R Date
datetime <- as.Date(myDatetime-1, origin="1899-12-31")
# now convert to POSIXct
(posixct <- .POSIXct(unclass(datetime)*86400, tz="GMT"))
# [1] "2012-08-28 00:06:40 GMT" "2012-08-28 00:14:01 GMT"
# [3] "2012-08-28 00:21:23 GMT" "2012-08-28 00:28:44 GMT"
# times are sometimes off by 1 second, add more digits to seconds to see why
options(digits.secs=6)
posixct
# [1] "2012-08-28 00:06:40.9823 GMT" "2012-08-28 00:14:01.9680 GMT"
# [3] "2012-08-28 00:21:23.0399 GMT" "2012-08-28 00:28:44.0256 GMT"
# round to nearest second
(posixct <- round(posixct, "sec"))
# [1] "2012-08-28 00:06:41 GMT" "2012-08-28 00:14:02 GMT"
# [3] "2012-08-28 00:21:23 GMT" "2012-08-28 00:28:44 GMT"
# now you can convert to your desired format
format(posixct, "%d.%m.%Y %H:%M:%S")
# [1] "28.08.2012 00:06:41" "28.08.2012 00:14:02"
# [3] "28.08.2012 00:21:23" "28.08.2012 00:28:44"

#2


2  

This gets close: The numbers indicate the number of seconds since a date close to 1/1/1900:

接近:数字表示自接近1/1/1900的日期以来的秒数:

as.POSIXct(x*3600*24, origin=as.Date("1900-01-01")-2, tz="UTC")
[1] "2012-08-28 01:06:40 BST" "2012-08-28 01:14:01 BST" "2012-08-28 01:21:23 BST"
[4] "2012-08-28 01:28:44 BST"

There still is a timezone offset in there.

那里仍然存在时区偏移。

#1


10  

You dates are in an Excel-like date format (days after January 1, 1900), so you need to convert them to an R date format. Then you can convert it to a datetime format (POSIXct).

您的日期采用类似Excel的日期格式(1900年1月1日之后的日期),因此您需要将它们转换为R日期格式。然后,您可以将其转换为日期时间格式(POSIXct)。

# first convert to R Date
datetime <- as.Date(myDatetime-1, origin="1899-12-31")
# now convert to POSIXct
(posixct <- .POSIXct(unclass(datetime)*86400, tz="GMT"))
# [1] "2012-08-28 00:06:40 GMT" "2012-08-28 00:14:01 GMT"
# [3] "2012-08-28 00:21:23 GMT" "2012-08-28 00:28:44 GMT"
# times are sometimes off by 1 second, add more digits to seconds to see why
options(digits.secs=6)
posixct
# [1] "2012-08-28 00:06:40.9823 GMT" "2012-08-28 00:14:01.9680 GMT"
# [3] "2012-08-28 00:21:23.0399 GMT" "2012-08-28 00:28:44.0256 GMT"
# round to nearest second
(posixct <- round(posixct, "sec"))
# [1] "2012-08-28 00:06:41 GMT" "2012-08-28 00:14:02 GMT"
# [3] "2012-08-28 00:21:23 GMT" "2012-08-28 00:28:44 GMT"
# now you can convert to your desired format
format(posixct, "%d.%m.%Y %H:%M:%S")
# [1] "28.08.2012 00:06:41" "28.08.2012 00:14:02"
# [3] "28.08.2012 00:21:23" "28.08.2012 00:28:44"

#2


2  

This gets close: The numbers indicate the number of seconds since a date close to 1/1/1900:

接近:数字表示自接近1/1/1900的日期以来的秒数:

as.POSIXct(x*3600*24, origin=as.Date("1900-01-01")-2, tz="UTC")
[1] "2012-08-28 01:06:40 BST" "2012-08-28 01:14:01 BST" "2012-08-28 01:21:23 BST"
[4] "2012-08-28 01:28:44 BST"

There still is a timezone offset in there.

那里仍然存在时区偏移。