Thanks, the example helps me to understand what you're looking for (I hope!).
It sounds like you want to update the StartDate field conditionally based on the current Sys.Date and Sys.time.
If the StartDate is not today, you want StartDate to be updated with the time 00:00:00.
If the StartDate is today, you want StartDate to show a time that is ten minutes after the next hour today. I presume the point of the if/else structure is that you want to pad the hour digits before 10 so that 8am shows up as 08 instead of just 8.
Given all that, the way I might approach the conversion this way. (I'm not so strong with data.table so this is a tidyverse solution.)
# LoadData ----------------------------------------------------------------
library(tidyverse)
#> -- Attaching packages -------------------------------------------------------------------------------------------------- tidyverse 1.2.1 --
#> v ggplot2 2.2.1.9000 v purrr 0.2.4
#> v tibble 1.4.1 v dplyr 0.7.4
#> v tidyr 0.8.0 v stringr 1.3.0
#> v readr 1.1.1 v forcats 0.3.0
#> -- Conflicts ----------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
#> x dplyr::filter() masks stats::filter()
#> x dplyr::lag() masks stats::lag()
upload_file<-data.frame(stringsAsFactors=FALSE,
sku = c("SA421WA00MIGINDFAS", "SA421WA00XZGINDFAS",
"SA421WA01BIJINDFAS", "SA421WA01CFPINDFAS",
"SA421WA01EBVINDFAS", "SA421WA01XZFINDFAS", "SA421WA02FPMINDFAS",
"SA421WA02KEKINDFAS", "SA421WA02XZEINDFAS",
"SA421WA03SRHINDFAS"),
specialPrice = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
discountPercent = c(35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L),
margin = c(49L, 43L, 49L, 49L, 43L, 43L, 43L, 49L, 43L, 43L),
StartDate = c("2018-03-13", "2018-03-12",
"2018-03-12", "2018-03-11",
"2018-03-01", "2018-03-02", "2018-03-12",
"2018-03-12", "2018-03-21", "2018-03-12"),
EndDate = c("2018-03-31", "2018-03-29",
"2018-03-31", "2018-03-30",
"2018-03-31", "2018-03-31", "2018-03-31",
"2018-03-31", "2018-03-28", "2018-03-30"),
vendorPD = c(35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L)
)
# Possible approach ---------------------------------------------------
currentHour <- lubridate::hour(Sys.time())
currentHour
#> [1] 11
upload_file2 <-
upload_file %>%
mutate(StartDate = lubridate::ymd(StartDate),
today = StartDate == Sys.Date(), #temporary flag b/c I had trouble getting the conditional to work inside case_when
StartDate = case_when(today ~ lubridate::ymd_hms(paste(StartDate, currentHour+1, 10, 0)),
TRUE ~ lubridate::ymd_hms(paste(StartDate, '00:00:00'))),
EndDate = lubridate::ymd_hms(paste(EndDate, '23:59:00'))) %>%
select(-today) %>%
mutate(StartDate = as.character(StartDate),
EndDate = as.character(EndDate))
upload_file2
#> sku specialPrice discountPercent margin
#> 1 SA421WA00MIGINDFAS NA 35 49
#> 2 SA421WA00XZGINDFAS NA 35 43
#> 3 SA421WA01BIJINDFAS NA 35 49
#> 4 SA421WA01CFPINDFAS NA 35 49
#> 5 SA421WA01EBVINDFAS NA 35 43
#> 6 SA421WA01XZFINDFAS NA 35 43
#> 7 SA421WA02FPMINDFAS NA 35 43
#> 8 SA421WA02KEKINDFAS NA 35 49
#> 9 SA421WA02XZEINDFAS NA 35 43
#> 10 SA421WA03SRHINDFAS NA 35 43
#> StartDate EndDate vendorPD
#> 1 2018-03-13 00:00:00 2018-03-31 23:59:00 35
#> 2 2018-03-12 12:10:00 2018-03-29 23:59:00 35
#> 3 2018-03-12 12:10:00 2018-03-31 23:59:00 35
#> 4 2018-03-11 00:00:00 2018-03-30 23:59:00 35
#> 5 2018-03-01 00:00:00 2018-03-31 23:59:00 35
#> 6 2018-03-02 00:00:00 2018-03-31 23:59:00 35
#> 7 2018-03-12 12:10:00 2018-03-31 23:59:00 35
#> 8 2018-03-12 12:10:00 2018-03-31 23:59:00 35
#> 9 2018-03-21 00:00:00 2018-03-28 23:59:00 35
#> 10 2018-03-12 12:10:00 2018-03-30 23:59:00 35