Convert DD/MM/YYYY HH:MM into date - r

I need to transfer a date time read in as a character from a CSV and convert it to a POSIXct format.
I can successfully do with dates only but have been unable to do this for a date and a time combined character string, the time.
time <-('01/08/2014 16:25')
as.POSIXct(time, origin = "03/01/1950 00:00", tz = "GMT")
[1] "0001-08-20 GMT"
class(time2)
[1] "POSIXlt" "POSIXt"
Any pointers would be appreciated!

time <-("01/08/2014 16:25:00")
time2 <- strptime(time,"%d/%m/%Y %H:%M:%S",tz="GMT")
[1] "2014-08-01 16:25:00 GMT"
I was unaware that the %Y has to be capped for 4 digits!

Related

How to turn PM/AM time stamp am/pm

I have a dataset with a column where date and time is stored.
The data I have is:
03/17/2020 09:30:00 PM
I want to convert AM/PM to a 24hour format.
My attempt was using this:
as.POSIXct(df$Date, format="%d/%m/%Y %I:%M:%S %p", tz="UTC")
When I run this with the whole dataset, the majority of dates turns into "NA".
Why is this happening? I am really confused.
Using lubridate:
x <- "03/17/2020 09:30:00 PM"
lubridate::mdy_hms(x)
[1] "2020-03-17 21:30:00 UTC"
Using as.Posixct: note that you need the month / day convention, not the day/month:
as.POSIXct(x, format="%m/%d/%Y %I:%M:%S %p", tz = "UTC")
[1] "2020-03-17 21:30:00 UTC"

Converting long integer into date and time in r [duplicate]

This question already has answers here:
Convert integer to class Date
(3 answers)
Closed 1 year ago.
I have date and time information in the following format:
z <- 20201019083000
I want to convert it into a readable date and time format such as follows:
"2020-10-19 20:20"
So far I have tried this but cannot get the correct answer.
#in local
as.POSIXct(z, origin = "1904-01-01")
"642048-10-22 14:43:20 KST"
#in UTC
as.POSIXct(z, origin = "1960-01-01", tz = "GMT")
"642104-10-23 05:43:20 GMT"
#in
as.POSIXct(as.character(z), format = "%H%M%S")
"2021-07-13 20:20:10 KST"
Any better way to do it?
library(lubridate)
ymd_hms("20201019083000")
# [1] "2020-10-19 08:30:00 UTC"
# or, format the output:
format(ymd_hms("20201019083000"), "%Y-%m-%d %H:%M")
# "2020-10-19 08:30"
You can use as.POSIXct or strptime with the format %Y%m%d%H%M%S:
as.POSIXct(as.character(z), format="%Y%m%d%H%M%S")
#[1] "2020-10-19 08:30:00 CEST"
strptime(z, "%Y%m%d%H%M%S")
#[1] "2020-10-19 08:30:00 CEST"
Your tried format "%H%M%S" dos not include Year %Y , Month %m and Day %d.

Including seconds when using strptime with examples such as 10-10-2010 00:00:00 [duplicate]

This question already has answers here:
How can I keep midnight (00:00h) using strptime() in R?
(2 answers)
Closed 3 years ago.
I have had a good hunt around and sure this has to have been answered before but I cant seem to find any help!
I have a series of times in a data frame, some of which have the following time stamp in the following format:
Date <- '2018-10-10'
Time <- '00:00:00'
When I use the strptime function it returns only the date, it removes the 00:00:00, see below:
datetime <- strptime(paste(Date,Time),
format = "%Y-%m-%d %H:%M:%S",
tz = 'GMT')
> datetime
[1] "2018-10-10 GMT"
if for example it was Time <- 00:00:01 it would return
> datetime
[1] "2018-10-10 00:00:01 GMT"
Does anyone know a way of ensuring the output for 00:00:00 instances are displayed. Desired output:
"2018-10-10 00:00:00 GMT"
Many thanks!!
Jim
When you type datetime and hit <Enter>, R will use a/the suitable print method to display datetime. Just because datetime returns "2018-10-10 GMT" doesn't mean that datetime has forgotten about the seconds.
To ensure a consistent format of your POSIXlt object, you could use format
format(datetime, "%Y-%m-%d %H:%M:%S", usetz = T)
#[1] "2018-10-10 00:00:00 GMT"
Similar for case 2
Date <- '2018-10-10'
Time <- '00:00:01'
datetime <- strptime(paste(Date,Time), format = "%Y-%m-%d %H:%M:%S", tz = 'GMT')
format(datetime, "%Y-%m-%d %H:%M:%S", usetz = T)
#[1] "2018-10-10 00:00:01 GMT"
Sample data
Date <- '2018-10-10'
Time <- '00:00:00'
datetime <- strptime(paste(Date,Time), format = "%Y-%m-%d %H:%M:%S", tz = 'GMT')

POSIXct not accepting custom origin

I feel like I'm missing something obvious here. I'm importing some data which is stored as HH:MM:SS. I'm trying to convert this to POSIXct and manually specify the origin as the date the data was collected.
datIn$TimeComplete <- as.POSIXct(datIn$Time, format="%H:%M:%S", origin="2000-01-01", tz="CET")
The output of this registers the HH:MM:SS correctly but says the day is 2019-03-05 (today) and I can't seem to convince it to do anything different.
You are misunderstanding the concept of origin. Origin is there to help convert numbers to dates. Those numbers represent seconds so you need the origin in order to add those seconds to the origin and get the datetime object. For example,
as.POSIXct(60, tz = "GMT", origin = '2015-03-05')
#[1] "2015-03-05 00:01:00 GMT"
as.POSIXct(3600, tz = "GMT", origin = '2015-07-05')
#[1] "2015-07-05 01:00:00 GMT"
What you are trying to do can be easily achieved by pasting the desired date to your times and converting to datetime, i.e.
as.POSIXct(paste0('2000-01-01 ', '11:03:15'), format = "%Y-%m-%d %H:%M:%S", origin = "", tz = "CET")
#[1] "2000-01-01 11:03:15 CET"

How to convert year into %Y%m%d %H:%M:%S?

I have a year list (format date) and I would to transform this year into a %Y%m%d %H:%M:%S format and the month must be 01 and day must be 01 as in the example below :
1800 transformed in 1800/01/01 00:00:00
Anybody as a solution ?
Try zoo library:
library(zoo)
> (date <- as.POSIXct(as.yearmon(2010)))
[1] "2010-01-01 GMT"
> format(date, "%Y/%m/%d %H:%M:%S")
[1] "2010/01/01 00:00:00"
EDIT: #user3370470 Please explain yourself. The below confirm what I've been saying so far.
> format(as.POSIXct(as.yearmon(1800)), "%Y/%m/%d %H:%M:%S")
[1] "1800/01/01 00:00:00"
> format(as.yearmon(1800), "%Y/%m/%d %H:%M:%S")
[1] "1800/01/01 00:00:00"
The result of as.POSIXct(as.yearmon(1800) is a datetime object, class POSIXct.
I don't know of a date format - there's a Date class, do you mean that. If the only info you have is the year, then why not sprintf("%s/01/01 00:00:00", charYear) where charYear is a vector of string representations of the year.

Resources