I have a POSIXct variable in the form of "2000-01-01 01:01:01" (ymd hms) but I'd like to drop the ymd so I'm left with only the time (i.e. "01:01:01").
We can use as.hms from
library(hms)
as.hms(as.POSIXct(str1))
#01:01:01
If we need only the string version, format would work as well
format(as.POSIXct(str1), "%H:%M:%S")
#[1] "01:01:01"
data
str1 <- "2000-01-01 01:01:01"
We can also use chron library:
chron::times(strftime(as.POSIXct("2000-01-01 01:01:01"),"%H:%M:%S"))
# [1] 01:01:01
Related
My date value is in this format
02:27:16 05-Mar-2019, Tue stored in Assigned date column
Am converting
srdetails1$Assigned On GMT<-as.POSIXct(srdetails1$Assigned On GMT, tz="", format = "%H:%M:%S %m/%d/%Y")
srdetails$Assigned On GMT
the value get converted as
43497.067407407405
Instead of showing a date and any function i use on this column for
e.g :-
day(ymd_hms() etc gives me "NA"
How do i resolve this - Any help appreciated
When i trim the date with only m/d/y (without time) it works properly
Your format mask does not match the timestamp which you are trying to use with as.POSIXct. Consider the following version:
x <- "02:27:16 05-Mar-2019"
as.POSIXct(x, tz="", format = "%H:%M:%S %d-%b-%Y")
[1] "2019-03-05 02:27:16 CET"
We can use anytime
library(anytime)
addFormats("%H:%M:%S %d-%b-%Y")
anytime(x)
#[1] "2019-03-05 02:27:16 EST"
data
x <- "02:27:16 05-Mar-2019"
I am looking to convert this character vector to a date format
I have tried various methods though have not been successful so far. Any assistance with this is greatly appreciated!
See ?strptime for format details
as.Date(x, "%d%b%Y:%H:%M:%S")
#[1] "2016-01-11"
Or if you want it in date-time format
as.POSIXct(x, format = "%d%b%Y:%H:%M:%S")
#[1] "2016-01-11 GMT"
With lubridate
library(lubridate)
dmy_hms(x)
#[1] "2016-01-11 UTC"
and if you want only date
ymd(dmy_hms(x))
#[1] "2016-01-11"
data
x <- "11JAN2016:00:00:00.000"
One option is anydate from anytime
library(anytime)
anydate(data)
#[1] "2016-01-11"
data
data <- "11JAN2016:00:00:00.000"
I have searched but I could not find out how to convert a date from a character string formatted as follows:
date <- "07-21-2015-09:30AM"
I wanted to use as.Date, but I have not manage to. All I get is the following:
as.Date(date, format="%m-%d-%y-%hAM")
NA
as.Date(dates, format="%m-%d-%y-%h")
NA
If we need the 'date' and 'time', one option is as.POSIXct
as.POSIXct(date, format='%m-%d-%Y-%I:%M%p')
#[1] "2015-07-21 09:30:00 EDT"
You can also use the lubridate package like this:
library('lubridate')
date <- "07-21-2015-09:30AM"
mdy_hm(date)
# "2015-07-21 09:30:00 UTC"
I like strptime for this:
strptime(date, format="%m-%d-%Y-%R%p")
#[1] "2015-07-21 09:30:00 EDT"
And in the case that you needed to see the date in the same format as entered, you can call the related strftime. It doesn't change the internal storage of the variable, rather it changes the format only.
strftime(xx, format="%m-%d-%Y-%R%p")
#[1] "07-21-2015-09:30AM"
I have searched but I could not find out how to convert a date from a character string formatted as follows:
date <- "07-21-2015-09:30AM"
I wanted to use as.Date, but I have not manage to. All I get is the following:
as.Date(date, format="%m-%d-%y-%hAM")
NA
as.Date(dates, format="%m-%d-%y-%h")
NA
If we need the 'date' and 'time', one option is as.POSIXct
as.POSIXct(date, format='%m-%d-%Y-%I:%M%p')
#[1] "2015-07-21 09:30:00 EDT"
You can also use the lubridate package like this:
library('lubridate')
date <- "07-21-2015-09:30AM"
mdy_hm(date)
# "2015-07-21 09:30:00 UTC"
I like strptime for this:
strptime(date, format="%m-%d-%Y-%R%p")
#[1] "2015-07-21 09:30:00 EDT"
And in the case that you needed to see the date in the same format as entered, you can call the related strftime. It doesn't change the internal storage of the variable, rather it changes the format only.
strftime(xx, format="%m-%d-%Y-%R%p")
#[1] "07-21-2015-09:30AM"
My date information is a string in the following format: 3/12/1956 0:00:00
I have tried converting it using DOB<-as.Date(DOB, "%d/%m/%y %H:%M:%S")
I am trying to convert it for the purpose of then applying the age_calc function in eeptools package.
Is there some other way to change a non standard format into a date. Damn Aussie dates!
You can try lubridate as an alternative
library(lubridate)
DOB <- '3/12/1956 0:00:00'
mdy_hms(DOB)
#[1] "1956-03-12 UTC"
It can also take multiple formats
DOB <- c('3/12/1956 0:00:00', '3.12/1956 0.00/00')
mdy_hms(DOB)
#[1] "1956-03-12 UTC" "1956-03-12 UTC"
Or as #Richard Scriven commented,
as.Date(DOB, "%d/%m/%Y %H:%M:%S")
#[1] "1956-12-03"