String Simple

d1 = date()
d1
## [1] "Tue Mar 24 16:39:47 2015"
class(d1)
## [1] "character"
d2 = Sys.Date()
d2
## [1] "2015-03-24"
class(d2)
## [1] "Date"

Formatting Dates

format(d2,"%a %b %d")
## [1] "화 3 24"
Sys.getlocale()
## [1] "LC_COLLATE=Korean_Korea.949;LC_CTYPE=Korean_Korea.949;LC_MONETARY=Korean_Korea.949;LC_NUMERIC=C;LC_TIME=Korean_Korea.949"
Sys.setlocale("LC_TIME", "C")
## [1] "C"
format(d2,"%a %b %d")
## [1] "Tue Mar 24"
Sys.setlocale("LC_TIME", "Korean_Korea")
## [1] "Korean_Korea.949"
format(d2,"%a %b %d")
## [1] "화 3 24"

Creating Dates

Sys.setlocale("LC_TIME", "C")
## [1] "C"
x = c("1jan1960","2jan1960","31mar1960","30jul1960");z = as.Date(x,"%d%b%Y")
z
## [1] "1960-01-01" "1960-01-02" "1960-03-31" "1960-07-30"
z[1] - z[2]
## Time difference of -1 days
as.numeric(z[1] - z[2])
## [1] -1

Convering to Julian

weekdays(d2)
## [1] "Tuesday"
months(d2)
## [1] "March"
julian(d2)
## [1] 16518
## attr(,"origin")
## [1] "1970-01-01"
#attr(,"origin")

Lubridate

library(lubridate); ymd("20140108")
## [1] "2014-01-08 UTC"
mdy("08/04/2013")
## [1] "2013-08-04 UTC"
dmy("03-04-2013")
## [1] "2013-04-03 UTC"
ymd("2015-03-23")
## [1] "2015-03-23 UTC"
ymd_hms("2015-03-23 11:43:12")
## [1] "2015-03-23 11:43:12 UTC"
ymd_hms("2015-03-23 11:43:12", tz="Asia/Seoul")
## [1] "2015-03-23 11:43:12 KST"