I wrote code to extract the date from a given string. Given
> "Date: 2012-07-29, 12:59AM PDT"
it extracts
> "2012-07-29"
The problem is my code looks lengthy and cumbersome to read. I was wondering if was a more elegant way of doing this.
raw_date = "Date: 2012-07-29, 12:59AM PDT"
#extract the string from raw date
index = regexpr("[0-9]{4}-[0-9]{2}-[0-9]{2}", raw_date) #returns 'start' and 'end' to be used in substring
start = index #start represents the character position 's'. start+1 represents '='
end = attr(index, "match.length")+start-1
date = substr(raw_date,start,end); date
You can use
strptime()to parse time objects:Note that I shifted your input string as I am unsure that 12:59AM exists… Just to prove the point, shifted by three hours (expressed in seconds, the base units):
Oh, and if you just want the date, it is of course even simpler: