Change Timestamp in R [duplicate] - r

This question already has answers here:
Round a POSIX date (POSIXct) with base R functionality
(3 answers)
How to drop minutes in R?
(2 answers)
Closed 4 years ago.
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.

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())

Related

R: converting original excel dates to the format yyyy-mm-dd HH:MM:SS [duplicate]

This question already has answers here:
Converting excel DateTime serial number to R DateTime
(5 answers)
Closed 7 months ago.
I've read an excel files with dates in different formats into R. Some are correctly read in the format "yyyy-mm-dd HH:MM:SS" and those who have had another format in excel before are now numbers like:
44586.727083333302 (stands for 25.01.2022 17:27:00)
I tried to convert them:
as.Date(df$dates, format='%Y-%m-%d %H:%M:%S', origin = "1900-01-01 24:00:00")
But R gives me just yyyy-mm-dd and HH:MM:SS is missing.
I need the timestamp as well. Does anyone know how the code must be?
You'll have to use the as.POSIXct() function instead of the as.Date() function. as.Date() returns the day without the time. The formatting you did should be the same.
Update: request OP:
You should install parsedate package:
library(parsedate)
#your df:
date
1 2018-06-30 12:09:34
2 44586.727083333302
# with this code:
dat %>%
mutate(x = parse_date(date))
you get this
date x
1 2018-06-30 12:09:34 2018-06-30 12:09:34
2 44586.727083333302 2022-07-31 15:39:06
First answer:
We could use the convertDateTime function:
library(openxlsx)
string <- 44586.727083333302
convertToDateTime(string, origin = "1900-01-01")
#or for your data frame:
convertToDateTime(df$dates, origin = "1900-01-01")
[1] "2022-01-25 17:27:00 CET"

How to convert datetime from character to normal datetime in R [duplicate]

This question already has an answer here:
Milliseconds in POSIXct Class
(1 answer)
Closed 1 year ago.
I have a dataframe in r which consists of a column with the following DateTime format with character class.
dt<-2021-03-01 06:10:31.346
I need to convert it into the following format with datetime class.
2021-03-01 06:10:31
I have tried the following solution but it didn't work.
Df$Dt<- as.POSIXct(Df$Dt,format="%Y-%m-%dT%H:%M:%S")
The T is not needed as it was not present in the original string
as.POSIXct(dt, format="%Y-%m-%d %H:%M:%S")
#[1] "2021-03-01 06:10:31 CST"
If we want to take care of the microseconds
as.POSIXct(dt, format="%Y-%m-%d %I:%M:%OS")
Also, the format is not really needed here as the input is in the accepted format for as.POSIXct
as.POSIXct(dt)
#[1] "2021-03-01 06:10:31 CST"
data
dt <- '2021-03-01 06:10:31.346'

R - Convert Float number to Date [duplicate]

This question already has answers here:
How to convert a numeric value into a Date value
(4 answers)
Closed 2 years ago.
Wondering if you can give me some light about an outstanding problem I have in R, version 4.0.2. I am moving data from an excel file in a given sharepoint to a local DB. The problem facing is that when reading the file using readxl the columns in the original file as dates (datetime) are being returned as floating point numbers. I need to change the number back to datetime.
Libraries used:
library(readxl)
library(odbc)
library(lubridate)
Number
44047.8149884259
44055.2403009259
44048.504537037
Expected result
8/4/2020 7:33:35 PM
8/12/2020 5:46:02 AM
8/5/2020 12:06:32 PM
I've tried to use as_date with different formats and as.POSIXct but don't seem to have an answer.
Thanks in advance for your kindly inputs.
We could use
format(as.POSIXct(v1 * 60 *60 * 24, origin = '1899-12-30', tz = 'UTC'),
'%m/%d/%Y %I:%M:%S %p')
#[1] "08/04/2020 07:33:34 pm" "08/12/2020 05:46:01 am" "08/05/2020 12:06:31 pm"
data
v1 <- c(44047.8149884259, 44055.2403009259, 44048.504537037)

Number of seconds to date conversion [duplicate]

This question already has answers here:
Convert UNIX epoch to Date object
(2 answers)
Closed 5 years ago.
I have a data set where one of the columns is sales date. Don't know why, but R converts it to numeric why performing any operation. I would like to convert it back to POSIXct date format in R. To do the same, I am using below code, but getting an unexpected result
x= as.Date(1448208000, origin = "1970-01-01")
[1] "3967028-10-31"
x= as.POSIXct(x,"%Y-%m-%d")
I am not good with dates format in R and would appreciate any kind of help in this regard.
1448208000 is the number of seconds since the unix epoch, and is the numeric representation of a POSIX object. To convert it back to POSIXct you want
as.POSIXct(1448208000, origin = "1970-01-01")
You'll also probably want to ensure the timezone is correct too; see the difference between these two commands
as.POSIXct(1448208000, origin = "1970-01-01", tz = "UTC")
# [1] "2015-11-22 16:00:00 UTC"
as.POSIXct(1448208000, origin = "1970-01-01", tz = "Australia/Melbourne")
# [1] "2015-11-23 03:00:00 AEDT"

R datetime scaling [duplicate]

This question already has an answer here:
Extract time from datatime in R
(1 answer)
Closed 6 years ago.
My dataset df has datetime column,
this column's format is YYYY-MM-DD HH:MM
I made new column that has only YYYY-MM-DD using
df$date <- factor(as.Date(df$datetime))
but I can't make column that has only time,
I want to make new column 'time' that has format HH:MM
Use strptime and strftime, the former convert character vectors to POSIXct and POSIXlt class( or date time class) and the latter does the opposite. With the format parameter, you can easily convert and format your datetime:
s = "2016-01-01 10:20"
strftime(strptime(s, "%Y-%m-%d %H:%M"), "%H:%M")
# [1] "10:20"
strftime(strptime(s, "%Y-%m-%d %H:%M"), "%Y-%M-%d")
# [1] "2016-20-01"

Resources