Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to parse a date string like 1/12/2014 which represents the first of december 2014. However, looking at http://www.statmethods.net/input/dates.html it looks like R would only support parsing something like 01/12/2014. Is this correct? How could I parse the date mentioned above?
Update: I used y instead of Y for the year and the date got parsed as "2020-12-01" --> everything working fine now.
No.
as.Date("1/12/2014", format="%d/%m/%Y")
See the Details section of ?strptime for an extensive list of the components of date-time that R can format.
lubridate makes handling dates easy. Use dmy for dates in day-month-year format.
library(lubridate)
dmy("1/12/2014")
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to parse a timestamp of the format:
2021-03-31T13:38:14
So I use the layout format of: 2006-02-15T15:04:05
For some reason all of my datetime objects are parsed correctly except 2021-03-31T13:38:14 and 2021-03-29T14:21:41. I get an error saying:
parsing time "2021-03-31T13:38:14": hour out of range
Why is this happening? When the hour is not out of range?
Here's the code:
const DateFormat = "2006-02-15T15:04:05"
var toParse = "2021-03-31T13:38:14"
submittedAt, err := time.Parse(DateFormat, toParse)
The format for day of the month is 02 and month is 01. So your layout format should be 2006-01-02T15:04:05
Check the other formats to see the pattern
https://golang.org/pkg/time/#pkg-constants
For instance RFC3339 is 2006-01-02T15:04:05Z07:00.
https://play.golang.org/p/ZHu6nbPtsJt
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Is there a way to change the date format in R. Example, when I tried with below formula to convert, it was not showing a right result
format(as.Date("30-01-2021"), "%Y/%d/%m")
"0030/20/01"
Expected output
"2021-01-30"
The lubridate package works really well for this kind of formatting.
#Load library
library('lubridate')
# Convert date format
lubridate::dmy("30-01-2021")
The reason it is not working is because your date string is not in the default date format. The default is "%Y-%m-%d" but your string is "%d-%m-%Y". So you can fix it by specifying the input date format on the as.Date() function. Like this:
as.Date("30-01-2021", "%d-%m-%Y")
# [1] "2021-01-30"
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm working with a dataframe that has a column of dates, all of which are integers in the form 20210117 for January 17, 2021.
I tried to run
data$DATE <- as.Date(as.character(data$DATE), "%Y%m%d")
but this turns all my dates into NAs. What am I doing wrong and is there a way to fix this in base R without importing libraries?
You can use the format option when calling as.Date:
dates <- as.character(data$YearMonthDay)
data$newVar = as.Date(dates, format="%Y%m%d")
Note that you don't need to use the origin parameter here, because even though you are passing in numerical data, it is in the form of an actual text date. origin is used when passing in number of days since an epoch.
It is generally not advisable to go in this direction, and in any case, base R date's have to have a year component. So, include the year component, and if you don't want to use it, then don't use it.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm new to R and trying to load some time series data but I'm stuck at the first hurdle.
I have a dataframe with a date column called Date. The date format of the data is: 23-May-16 (it appears like this in the R console when I print df). To read as date I'm trying:
df$Date <- as.Date(df$Date, "%dd-%bbb-%yy")
as per guidance here
which produces the value <NA> when it reads the data.
Try:
as.Date(df$Date,format="%d-%b-%y")
You only need to list those once:
as.Date("23-May-16", "%d-%b-%y")
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Can someone please tell me why this doesn't work:
as.Date("01/08/15", format = "%m/%d/%Y")
[1] "0015-01-08"
Thanks!
You need to use lower-case %y. Upper-case %Y requires a 4-digit year. (In my opinion, the entire command should have failed with an error message, but unfortunately it didn't.) See strptime(), which documents the format codes.
as.Date("01/08/15", format = "%m/%d/%y")
## [1] "2015-01-08"