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

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.

Related

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.

convert date with time formats [R]

how can i convert a date from a format like yyyymmdd H:M to yyyy-mm-dd H:M, basicaly from 20200101 00:00 to 2020-01-01 00:00. i have tried multiple as.Date formats and cant obtain the result i want
example of what i have :
dates <- c("20200101 00:00", "20200101 01:00")
want <- as.Date(have, format="%Y%m%d %H:%M")
the output:
> want<- as.Date(dates, format="%Y%m%d %H:%M")
> want
[1] "2020-03-01" "2020-03-01"
There's two pieces here. One is converting to date time class, such as POSIXt. Then there is how this object is printed. Under the hood it's all represented the same, but you can control how it's displayed.
The format argument in any of the conversion functions (as.Date or as_datetime) is describing how to parse the string representation into the components of a data time object (e.g. where in the string to find the minutes). You need to then use something like format or strftime to then control how the values are printed/displayed.
Below is what I think you're aiming for:
dates_as_strings <- c("20200101 00:00", "20200101 01:00")
dates_as_datetime_objs <- lubridate::as_datetime(dates_as_strings, format="%Y%m%d %H:%M")
strftime(dates_as_datetime_objs, "%Y-%m-%d %H:%M", tz = "UTC")
#> [1] "2020-01-01 00:00" "2020-01-01 01:00"
Created on 2021-05-21 by the reprex package (v1.0.0)

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

Convert from a timestamp containing abbreviated month and PM(in time) to Date

How can I convert from a timestamp like this:
aDate <- "9-Apr-17 10:00:00 PM"
convert <- as.Date(aDate, "%d-%b-%y %H:%M:%S")
Right now, convert gives NA.
This should solve your problem :
format(lubridate::dmy_hms(aDate), format = "%d-%m-%y %H:%M:%S")
"09-04-17 22:00:00"

Convert DD/MM/YYYY HH:MM into date

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!

Resources