1

I added a column to record the timestamp in R by using the following code

data$Date <- Sys.Date()      2018-08-10 18:06:21

This pastes a time-stamp with the current system date and time.

However, I want to set the time in the time-stamp to 00:00:00.

I tried using strptime and replacing the text. Thanks in advance.

marine8115
  • 588
  • 3
  • 22
  • 1
    I think you mean `Sys.time()` - `Sys.Date()` doesn't return a date + time. Also, you want `?trunc` (`?trunc.POSIXt` to be exact) – thelatemail Oct 10 '18 at 05:11
  • Sorry if I was unclear. I just want to change the `18:06:21` portion of the timestamp to `00:00:00` – marine8115 Oct 10 '18 at 05:13
  • Possible duplicate of https://stackoverflow.com/questions/11325631/round-a-posix-date-posixct-with-base-r-functionality or maybe even https://stackoverflow.com/questions/45745396/how-to-drop-minutes-in-r/45745554 – thelatemail Oct 10 '18 at 05:13
  • The `round` function truncates the time-stamp. I intend to have `00:00:00` as well – marine8115 Oct 10 '18 at 05:17
  • 1
    Nope, that's just how it prints to the screen, it's still stored as a datetime with 00:00:00 after rounding. You can use `format()` around the date if you need it all to be shown. – thelatemail Oct 10 '18 at 05:18

1 Answers1

3

There are multiple ways to do this , one way is to format your time part from Sys.time() to the required time.

data$Date <- format(Sys.time(), "%Y-%m-%d 00:00:00")

Sys.time returns current system date and time

Sys.time()
#[1] "2018-10-10 05:12:56 GMT"

format(Sys.time(), "%Y-%m-%d 00:00:00")
#[1] "2018-10-10 00:00:00"

Or if you want to use Sys.Date you can wrap it in as.POSIXct

data$Date <- as.POSIXct(Sys.Date())
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213