R convert string in date format - r

I'm having some problems converting string vectors into date format. I have found a lot of information here and here but they haven't worked in my case (R 3.2.3).
> strptime("Fri Feb 05 14:10:10 +0000 2016",
format="%a %b %d %H:%M:%S %z %Y", tz="GMT")
[1] NA

Related

Convert Uncommon Date Format

Is there an easy way to transform this string into a meaningful date format?
date <- "Tue Apr 04 10:18:33 +0000 2017"
Right now I would use some regex and then the lubridate package. But I guess there is a less complicated way.
Try this:
as.POSIXct(date, format = "%a %b %d %H:%M:%S %z %Y")

How to convert string to date format in R

I have a column of strings in the following format:
Wed, 6 Dec 2000 08:47:00 -0800 (PST)
How can I convert this into date format using lubridate or another package? I have done this before, but there was no -0800 (PST) at the end.
Thank you.
I was able to get a result using strptime() without even worrying about the timezone name at the end:
> x - "Wed, 6 Dec 2000 08:47:00 -0800 (PST)"
> strptime(x, "%a, %d %b %Y %H:%M:%S %z")
[1] "2000-12-07 00:47:00"
However, if you want to remove the timezone name, you can use substr() to do this:
> strptime(substr(x, 1, nchar(x)-6), "%a, %d %b %Y %H:%M:%S %z")
[1] "2000-12-07 00:47:00"
We can also use parse_date_time
library(lubridate)
parse_date_time(x, "adbY HMS z", tz = "US/Pacific")
#[1] "2000-12-06 08:47:00 PST"

strptime returning NA

I am getting NA value for my date string using strptime in R.
I looked at the various answers, but it didn't work.
Here is my code
startDate=strptime("Wed May 25 01:51:32 UTC 2016", format="%a %B %d %H:%m:%S %Z %Y", tz="UTC")
print(startDate)
Any help would be appreciated.
"%H:%m:%S" should be "%H:%M:%S". Once you change that, you'll get an error because %Z is not valid for input.
If all the datetime strings have UTC timezone, this will work:
R> strptime("Wed May 25 01:51:32 UTC 2016", "%a %B %d %H:%M:%S UTC %Y", "UTC")
[1] "2016-05-25 01:51:32 UTC"
If not, then you can extract the year and prepend it to the string, because strptime will ignore all characters after those specified by the format string.
R> dts <- "Wed May 25 01:51:32 UTC 2016"
R> dtf <- "%Y %a %B %d %H:%M:%S"
R> strptime(paste(substring(dts, nchar(dts)-3), dts), dtf, "UTC")
[1] "2016-05-25 01:51:32 UTC"

converting utc format to required format using python

I am trying to convert UTC format date format to required format using python. Simply I have the datetime in the format(Fri Dec 07 19:06:06 +0000 2012), I need to convert this into my format(2012-12-07 19:06:06:546 +0000)
Code:
created_at = "Fri Dec 07 19:06:06 +0000 2012"
d = datetime.strptime(created_at, '%a %b %d %H:%M:%S %z %Y')
date_object = d.strftime('%y-%m-%d %H:%M:%S')
result:
ValueError: 'z' is a bad directive in format '%a %b %d %H:%M:%S %z %Y'
The below link from python bugs says its fixed but i didn understand what is fixed and in which version i can use %z
http://bugs.python.org/issue6641
I am not able to use %z . Is there any other way to handle this??
Do it like that:
from datetime import datetime
TIMESTAMP = datetime.utcnow().strftime('%d/%m/%Y %H:%M:%S')

How do I convert dates in this format to a date class in R?

I have a spreadsheet full of data with dates that look like this:
Mon Jul 16 15:20:22 +0000 2012
Is there a way to convert these to R dates (preferably PST) without using regular expression or is there no other way? I'd appreciate ideas on doing this conversion efficiently.
Sure, just use strptime() to parse time from strings:
R> strptime("Mon Jul 16 15:20:22 +0000 2012",
+ format="%a %b %d %H:%M:%S %z %Y")
[1] "2012-07-16 10:20:22 CDT"
R>
which uses my local timezone (CDT). If yours is Pacific, you can set it explicitly as in
R> strptime("Mon Jul 16 15:20:22 +0000 2012",
+ format="%a %b %d %H:%M:%S %z %Y", tz="America/Los_Angeles")
[1] "2012-07-16 08:20:22 PDT"
R>
which looks right with a 7 hour delta to UTC.
There's nearly a verbatim example of how to do this in the Examples section of ?strptime:
# ?strptime example:
## An RFC 822 header (Eastern Canada, during DST)
strptime("Tue, 23 Mar 2010 14:36:38 -0400", "%a, %d %b %Y %H:%M:%S %z")
# your data...
strptime("Mon Jul 16 15:20:22 +0000 2012", "%a %b %d %H:%M:%S %z %Y")
This can also be done with lubridate package in tidyverse
library(lubridate)
parse_date_time("Mon Jul 16 15:20:22 +0000 2012", orders = "amdHMSzY")
which is what I prefer.

Resources