Converting chr “00:00:00” to date-time “00:00:00”

后端 未结 1 849
轮回少年
轮回少年 2021-01-05 07:31

My question comes from this question. The question had the following character string.

x <- \"2007-02-01 00:00:00\"
y <- \"02/01/2007 00:06:10\"
         


        
1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-05 08:03

    It is just the print that remove the precision if the time part of a date is a midnight. This is literlay explained in ??strftime help, specially the format parameter:

    A character string. The default is "%Y-%m-%d %H:%M:%S" if any component has a time component which is not midnight, and "%Y-%m-%d" otherwise

    One idea is to redefine the S3 method print for POSIXct object:

    print.POSIXct <- function(x,...)print(format(x,"%Y-%m-%d %H:%M:%S"))
    

    Now for your example if your print your x date(with midnight part) you get:

    x <- "2007-02-01 00:00:00"
    x <- as.POSIXct(x,tz=Sys.timezone())
    x
    [1] "2007-02-01 00:00:00"
    

    0 讨论(0)
提交回复
热议问题