Month Language in the As.Date Function

month language in the as.date function

It works for me... there is no italian as you overwrite it with df$Month <- months(as.Date(df$Date)) can you please give us your sessionInfo(), here is mine

sessionInfo()

R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252 LC_MONETARY=French_France.1252
[4] LC_NUMERIC=C LC_TIME=C

attached base packages:
[1] stats graphics grDevices datasets utils methods base

other attached packages:
[1] Rcpp_0.10.4 TeachingDemos_2.9 fastmatch_1.0-4 fasttime_1.0-0 data.table_1.8.9 bit64_0.9-2
[7] bit_1.1-10 vimcom_0.9-8

loaded via a namespace (and not attached):
[1] tools_3.0.1

try to set LC_TIME to C too with Sys.setlocale("LC_TIME", "C");

Add months in local language to date function

var $time = "Fri May 04 20:33:17 +0000 2018";

function relative_time(time_value) {
var date = new Date(time_value);
var months= ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio',
'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
return date.getDate()+ ' '+ months[date.getMonth()];
}

relative_time($time);

Does this answer your question?

function as.Date just recognizes german month - but not english month

If you are 'completely sure' that the language is German try this:

Sys.setlocale("LC_ALL","German")

test <- c("15MAI2006","01OKT2011")
as.Date(test, format='%d%B%Y')

[1] "2006-05-15" "2011-10-01"

However, you have in your original data "01OCT2011" and October should be Okt

How to set the default language of date in R

OK, as a Q&A site, it seems an answer is required. From your description, it appears to be the issue of your locales. Read ?locales for more.

You can have a test with this (read ?strptime for various format, and pay special attention to those sensitive to locales):

format(Sys.Date(), format = "%Y-%b-%d")
# [1] "2016- 9月-06"

The output has the month in Chinese. If I want to change the display, I need to set "LC_TIME" locale to "C":

Sys.setlocale("LC_TIME", "C")

Then it is OK:

format(Sys.Date(), "%Y-%b-%d")
# [1] "2016-Sep-06"

Every time you start a new R session, you get back to native setting. Should you want a permanent change, put

.First <- function() {
Sys.setlocale("LC_TIME", "C")
}

in the $(R RHOME)/etc/Rprofile.site file. Read ?Startup for how to customize R startup and the use of .First.

Converting year and month (yyyy-mm format) to a date?

Try this. (Here we use text=Lines to keep the example self contained but in reality we would replace it with the file name.)

Lines <- "2009-01  12
2009-02 310
2009-03 2379
2009-04 234
2009-05 14
2009-08 1
2009-09 34
2009-10 2386"

library(zoo)
z <- read.zoo(text = Lines, FUN = as.yearmon)
plot(z)

The X axis is not so pretty with this data but if you have more data in reality it might be ok or you can use the code for a fancy X axis shown in the examples section of ?plot.zoo .

The zoo series, z, that is created above has a "yearmon" time index and looks like this:

> z
Jan 2009 Feb 2009 Mar 2009 Apr 2009 May 2009 Aug 2009 Sep 2009 Oct 2009
12 310 2379 234 14 1 34 2386

"yearmon" can be used alone as well:

> as.yearmon("2000-03")
[1] "Mar 2000"

Note:

  1. "yearmon" class objects sort in calendar order.

  2. This will plot the monthly points at equally spaced intervals which is likely what is wanted; however, if it were desired to plot the points at unequally spaced intervals spaced in proportion to the number of days in each month then convert the index of z to "Date" class: time(z) <- as.Date(time(z)) .

Add months in local language to date function

var $time = "Fri May 04 20:33:17 +0000 2018";

function relative_time(time_value) {
var date = new Date(time_value);
var months= ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio',
'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
return date.getDate()+ ' '+ months[date.getMonth()];
}

relative_time($time);

Does this answer your question?



Related Topics



Leave a reply



Submit