R Converting from Datetime to Date

R Converting from datetime to date

We can use asPOSIXct to convert to DateTime

v1 <- as.POSIXct("12/9/2011 12:00:00 AM", format = "%d/%m/%Y %I:%M:%S %p")

If we need only Date

as.Date(v1)

How to convert a datetime to date in R, WITHOUT rounding the day?

Do your input dates include a timezone specification? If not, they are ambiguous and the rounding may be right or it may be wrong. If they do include a timezone specification, the lubridate package should handle them correctly.

I would advise against using tz = Sys.timezone() because that would make the interaction between input data and algorithm dependent on geography if your inputs don't include a timezone specification, so what works for you might not work for a different user in a different location.

Converting datetime character vector into date-time format

You can use as_datetime function from lubridate package

library(lubridate)
#> Warning: package 'lubridate' was built under R version 3.6.3
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
dttime = c("2021-08-03 11:59:59", "2021-08-03 12:59:59",
"2021-08-03", "2021-08-03 16:59:59")
as_datetime(dttime, tz = "UTC")
#> [1] "2021-08-03 11:59:59 UTC" "2021-08-03 12:59:59 UTC"
#> [3] "2021-08-03 00:00:00 UTC" "2021-08-03 16:59:59 UTC"

You can change the timezone into another timezone, see ?as_datetime

Convert date/time in date only in R

Or lubridate::dmy_hm("1/1/2016 0:00")

Converting datetime to date in R

Looks like it is a numeric in milliseconds from the unix-epoch (1970-01-01)... so first divide by 1000, en then make is a posix. Make sure to set the correct timezone!

For me (Netherlands), the data is presented in timezone "Europe/Amsterdam"... but this might differ for you...

as.POSIXct( 1114709760000/1000, origin = "1970-01-01", tz = "Europe/Amsterdam" )

[1] "2005-04-28 19:36:00 CEST"

Change of Date format

It should be %I to represent hours as decimal number (01–12), not %H, and %y to
years without century (00–99).

x <- "1/1/2021 12:00:00 AM"

format(strptime(x, "%m/%d/%Y %I:%M:%S %p"), "%m/%d/%y")
[1] "01/01/21"

Note that after you re-foramt the time object, it'll be a pure character string and lose all attributes of a time object.

Automatically convert formats of of date-time data to date only in r

How about this:

library(tidyverse)
library(lubridate)

``` r
library(tidyverse)
library(lubridate)

df %>%
mutate(time_temp = dmy_hms(time, quiet = TRUE)) %>%
mutate(time = if_else(is.na(time_temp),
dmy_hm(time, quiet = TRUE),
time_temp)) %>%
select(-time_temp)
#> # A tibble: 4 x 1
#> time
#> <dttm>
#> 1 2020-01-01 00:00:01
#> 2 2020-01-02 00:01:01
#> 3 2020-01-04 00:02:00
#> 4 2020-01-03 01:02:00

reprex data

df <- tibble(
time = c("01/01/2020 00:00:01", "02/01/2020 00:01:01", "04/01/20 00:02", "03/01/20 01:02")
)

Converting Character to Date Class in R

You can use lubridate's ymd_hms.

date <- "2019-03-12T14:32:24.000-01:00"
date1 <- lubridate::ymd_hms(date)
date1
#[1] "2019-03-12 15:32:24 UTC"

Note that timezone has changed to UTC now and hence you see a different time.

If you only want the date you can use as.Date and extract the time part with format.

as.Date(date1)
#[1] "2019-03-12"

format(date1, '%T')
#[1] "15:32:24"


Related Topics



Leave a reply



Submit