Convert to a date and time without a time zone?
本问题已经有最佳答案,请猛点这里访问。
我有一个时间,格式是
可以使用
1 | strptime("1/1/2010 10:00", format="%m/%d/%Y %H:%M", tz="Etc/GMT-7") |
(假设为月/日/年格式——其他格式选项见
假设您不希望时区出现在最终对象中,您可以尝试以下操作。我将此作为答案而不是对Flick先生的答案发表评论,因为如果我只在一个日期内尝试使用我的代码,它似乎不起作用。出于某种原因,我的代码似乎只在存在日期/时间的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | my.data <- read.csv(text=' id,date,time,latitude,longitud AA,3/16/2017,1:30,19.735,-156.085 AA,3/16/2017,2:57,19.800,-156.065 AA,3/16/2017,3:42,19.830,-156.057 AA,3/16/2017,17:31,19.963,-155.952 BB,3/16/2017,17:44,19.964,-155.951 BB,3/17/2017,2:46,19.985,-155.998 ', header = TRUE, stringsAsFactors = FALSE, sep = ',') my.data$date.time <- do.call(paste0, list(my.data$date, ' ', my.data$time)) my.data$date.time <- strptime(my.data$date.time,"%m/%d/%Y %H:%M", tz ="") my.data # id date time latitude longitud date.time #1 AA 3/16/2017 1:30 19.735 -156.085 2017-03-16 01:30:00 #2 AA 3/16/2017 2:57 19.800 -156.065 2017-03-16 02:57:00 #3 AA 3/16/2017 3:42 19.830 -156.057 2017-03-16 03:42:00 #4 AA 3/16/2017 17:31 19.963 -155.952 2017-03-16 17:31:00 #5 BB 3/16/2017 17:44 19.964 -155.951 2017-03-16 17:44:00 #6 BB 3/17/2017 2:46 19.985 -155.998 2017-03-17 02:46:00 str(my.data) #'data.frame': 6 obs. of 6 variables: # $ id : chr "AA""AA""AA""AA" ... # $ date : chr "3/16/2017""3/16/2017""3/16/2017""3/16/2017" ... # $ time : chr "1:30""2:57""3:42""17:31" ... # $ latitude : num 19.7 19.8 19.8 20 20 ... # $ longitud : num -156 -156 -156 -156 -156 ... # $ date.time: POSIXlt, format:"2017-03-16 01:30:00""2017-03-16 02:57:00""2017-03-16 03:42:00""2017-03-16 17:31:00" ... |