Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting date and hour from Posixct object with strftime

I was trying to extract date and hour from a date time column using strftime but I don't understand why the values returned are 1 hour previous than the values that should be returned. For example for a date time of 2013-01-01 00:00:00 the values returned should be 2013-01-01 00 but instead what I'm getting is 2012-12-31 23. I also tried adding 1 hour and then extracting but over a long sequence of dates, it is again disturbing the output. Please see this sample code for reference.

## creating the sequence of time steps for cleaned
start <- as.POSIXct('2013-01-01 00:00:00',tz='EST')
end <- as.POSIXct('2016-06-06 23:00:00',tz='EST')

timesteps = data.frame( seq.POSIXt(from = start, to =end , by = "5 min"))
colnames(timesteps) = "Time Index"

dateandhour = function (timeindex){
return(strftime(timeindex, format = "%Y-%m-%d %H"))
}

timesteps ['Date and Hour'] = sapply(timesteps$`Time Index`, dateandhour)

Please let me know what is it that I'm missing here. Thank you so much.

like image 849
Harshit Saxena Avatar asked Dec 22 '25 20:12

Harshit Saxena


1 Answers

That's because you are specifying the time zone in as.POSIXct, but not in strptime.

timesteps[1,1]
[1] "2013-01-01 EST"

 strftime(timesteps[1,1], format = "%Y-%m-%d %H")
[1] "2012-12-31 21"
 strftime(timesteps[1,1], format = "%Y-%m-%d %H",tz='EST')
[1] "2013-01-01 00"`

dateandhour = function (timeindex){
  return(strftime(timeindex, format = "%Y-%m-%d %H",tz='EST'))
}

timesteps ['Date and Hour'] = sapply(timesteps$`Time Index`, dateandhour)

head(timesteps)
           Time Index Date and Hour
1 2013-01-01 00:00:00 2013-01-01 00
2 2013-01-01 00:05:00 2013-01-01 00
3 2013-01-01 00:10:00 2013-01-01 00
4 2013-01-01 00:15:00 2013-01-01 00
5 2013-01-01 00:20:00 2013-01-01 00
6 2013-01-01 00:25:00 2013-01-01 00
like image 168
bjoseph Avatar answered Dec 24 '25 10:12

bjoseph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!