Convert Date to Posixct

Convert Date to POSIXct

Because as.POSIXct.Date doesn't look for a timezone (and won't pass it to .POSIXct if you specify it in ...) and Date objects are "UTC", so your POSIXct is offset from the UTC of the Date object.

It would be better to call as.POSIXct on the character string directly, if you can:

> as.POSIXct("2014-07-08", format="%Y-%m-%d")
[1] "2014-07-08 BRT"

Reconvert numeric date to POSIXct R

As the numeric vector is created from an object with time component, reconversion can also be in the same way i.e. first to POSIXct and then wrap with as.Date

as.Date(as.POSIXct(date1_num, origin = '1970-01-01'))
#[1] "2017-12-30"

convert Date to POSIXct from origin

We could use difftime to get the interval between a and origin.

a <- as.Date(as.POSIXct(1601510400, origin = "1970-01-01"))

as.numeric(difftime(time1 = as.POSIXct(a, tz = "UTC"),
time2 = as.POSIXct("1970-01-01", tz = "UTC"),
units = "secs"))
[1] 1601510400

Converting datetime from character to POSIXct object

For your real data issue replace the %m% with %m:

## Reading in the file:
fpath <- "c:/r/data/real_data.txt"
x <- read.csv(fpath, skip = 1, header = FALSE, sep = "", stringsAsFactors = FALSE)
names(x) <- c("date","time","bscat","scat_coef","pressure_mbar","temp_K","CH1","CH2") ## This is data from a Radiance Research Integrating Nephelometer Model M903 for anyone who is interested!

## issue was the %m% - fixed
x$datetime1 <- as.POSIXct(paste(x$date, x$time), format = "%Y-%m-%d %H:%M:%S", tz = "UTC")

## Here too - fixed
x$datetime2 <- strptime(paste(x$date, x$time), format = "%Y-%m-%d %H:%M:%S", tz = "UTC")
head(x)

Convert R Date to POSIXct Date in Database

Try to pass date as string, then convert to date in SQL, something like:

date <- "2020-01-01"

string <- paste0("SELECT * FROM D WHERE D_DATE >= TO_DATE('", date, "', 'YYYY-MM-DD')")
string
# [1] "SELECT * FROM D WHERE D_DATE >= TO_DATE('2020-01-01', 'YYYY-MM-DD')"

We might need to truncate the D_DATE: TRUNC(D_DATE)

How to convert a character column into date using as.POSIXct?

You may use different functions to make your time/date variables. Pay attention to the typeof (i.e., data storage mode in the memory) and class (i.e., object classes):

date1 <- as.Date(
c("2019-01-01 14:22","2019-01-01 16:08", "2019-01-01 07:16"),
format = "%Y-%m-%d %H:%M"
)
> typeof(date1)
[1] "double"
> class(date1)
[1] "Date"

date2 <- as.POSIXct(
c("2019-01-01 14:22","2019-01-01 16:08", "2019-01-01 07:16"),
format = "%Y-%m-%d %H:%M"
)
> typeof(date2)
[1] "double"
> class(date2)
[1] "POSIXct" "POSIXt"

date3 <- strptime(
c("2019-01-01 14:22","2019-01-01 16:08", "2019-01-01 07:16"),
format = "%Y-%m-%d %H:%M"
)
> typeof(date3)
[1] "list"
> class(date3)
[1] "POSIXlt" "POSIXt"

They may be shown as in double quotes " ":

[1] "2019-01-01 14:22:00 +0330" "2019-01-01 16:08:00 +0330"
[3] "2019-01-01 07:16:00 +0330"

But they do not have a character typeof.

Convert character date with timestamp to class POSIXct POSIXt

With lubridate, it can be dmy_hms

library(data.table)
library(lubridate)
DT2[, study_date := dmy_hms(study_date)]

Or using as.POSIXct

DT2[, study_date := as.POSIXct(study_date, format = '%d%b%Y:%H:%M:%S')]

How to convert date & time to numeric by using POSIXct and how to calculate the time?

Additionally, you can also use mdy_hm function from lubridate -

library(dplyr)
library(lubridate)

df <- df %>% mutate(across(c(starttime, stoptime), mdy_hm))

In base R, you can use as.POSIXct

df[1:2] <- lapply(df[1:2], as.POSIXct, format = "%m/%d/%Y %H:%M")


Related Topics



Leave a reply



Submit