[R] determine the year of a date

Boris Steipe boris.steipe at utoronto.ca
Wed Feb 3 19:34:40 CET 2016


Sorry - messed up example: corrected here...

d <- "7/27/77"
strptime(d, format="%m/%d/%y")  #  "1977-07-27 EDT"
x <- strptime(d, format="%m/%d/%y")

strftime(x, format="%m/%d/%Y")  # "07/27/1977"


On Feb 3, 2016, at 1:29 PM, Boris Steipe <boris.steipe at utoronto.ca> wrote:

> It seems your autocorrect is playing tricks on you but if I understand you correctly you have a two digit year and want to convert that to a four digit year? That's not uniquely possible of course; by convention, as the documentation to strptime() says:
> 
>  On input, values 00 to 68 are prefixed by 20 and 69 to 99 by 19 – that
>  is the behaviour specified by the 2004 and 2008 POSIX standards...
> 
> If this is correct for you, you need to convert the string to a time object and the time object back to string. The format specifier %Y prints four-digit years:
> 
> 
> d <- "7/27/59"
> strptime(d, format="%m/%d/%y")   # "2059-07-27 EDT"
> x <- strptime(d, format="%m/%d/%y")
> 
> strftime(x, format="%m/%d/%Y")   # "07/27/1977"
> 
> 
> 
> B.
> 
> 
> 
> 
> 
> 
> 
> On Feb 3, 2016, at 11:03 AM, carol white via R-help <r-help at r-project.org> wrote:
> 
>> Hi,might be trivial but how to determine the year of a date which is in the %m/%d/%y format and those whose year is century should be modified to ISO so that all date will have with year in ISO?
>> Regards,
>> Carol
>> 
>> 	[[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list