1

I have the following javascript code

date.getMonth());

This returns me the name of the month in English, how can I get the languages in Italian or in an another language. What does the language option depend on? I mean, what determines the language of the returned variable, and how can I change this language?

My full code is as follows;

var currentTime = new Date() 
var minDate = new Date(currentTime.getYear(), currentTime.getMonth()-1); 
var maxDate =  new Date(currentTime.getFullYear(), currentTime.getMonth());
Mixcels
  • 889
  • 1
  • 11
  • 23
Gunnit
  • 1,064
  • 5
  • 22
  • 45
  • 1
    *"`date.getMonth())` This returns me the name of the month in English"* No, it doesn't. If you remove the extra `)`, it returns the month as a zero-based number (0 for January, 1 for February...). – T.J. Crowder Sep 11 '13 at 09:07

3 Answers3

3

I believe that instead of reinventing the wheel you can use a solid library.

Moment.js

A javascript date library for parsing, validating, manipulating, and formatting dates.

http://momentjs.com/

If you really need to rewrite it then simply create an array of month names and based on the number of month pick corresponding name from the array.

Community
  • 1
  • 1
Emil A.
  • 3,387
  • 4
  • 29
  • 46
2

The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.

then create a month array as per required language..

Try this:-

<html>
<body>

<p id="demo">Click the button to display the name of this month.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
var month=new Array();
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";

var d = new Date();
var x = document.getElementById("demo");
x.innerHTML=month[d.getMonth()];
}
</script>

</body>
</html>

Hope this will help you..

Vikash Pandey
  • 5,407
  • 6
  • 41
  • 42
0

Date.getMonth() does not return the name of the month, it returns an integer 0..11 representing January..December

If you want the name of the month in your locale language, then see this SO question and the different answers Get month name from Date

I find this particular answer https://stackoverflow.com/a/16089495/30447 specially useful for your case.

Community
  • 1
  • 1
PA.
  • 28,486
  • 9
  • 71
  • 95