Fecha JavaScript método getMonth()

A continuación se muestra el ejemplo del método Date getMonth()
 

  • Ejemplo: 
     

javascript

<script type="text/javascript">
   // Creating a Date Object
   var DateObj = new Date('October 15, 1996 05:35:32');
 
   // Month from above Date Object is
   // Being extracted using getMonth()
   var months = DateObj.getMonth();
 
   // Printing month.
   document.write(months);
</script>
  • Producción: 
     
9

El método date.getMonth() se usa para obtener el mes (0 a 11) de un objeto Date dado.
Sintaxis: 
 

DateObj.getMonth()

Parámetro: Esta función no acepta ningún parámetro. 
Valor devuelto: Devuelve el Mes para el objeto Fecha dado. El mes es un valor entero que va de 0 a 11. Cero (0) significa enero, 1 significa febrero y así hasta el 11 significa diciembre.
Más códigos para el método anterior son los siguientes:
Programa 1: aquí la fecha del mes debe estar entre 1 y 31 porque ninguna fecha puede tener un mes mayor que 31. Es por eso que devuelve NaN, es decir, no es un número si el mes en el objeto Date es mayor que 31. 
 

javascript

<script type="text/javascript">
   // Creating a Date Object
   var DateObj = new Date('October 33, 1996 05:35:32');
 
   // Month from above Date Object is being
   // Extracted using getMonth()
   var months = DateObj.getMonth();
 
   // Printing month.
   document.write(months);
</script>

Producción: 
 

NaN

Programa 2: Si no se da el mes, devuelve cero (0). 
 

javascript

<script type="text/javascript">
   // Creating a Date Object
   var DateObj = new Date('1996 05:35:32');
 
   // Month from above Date Object is being
   // Extracted using getMonth()
   var months = DateObj.getMonth();
 
   // Printing month.
   document.write(months);
</script>

Producción: 
 

0

Programa 3: Si no se da nada como parámetro, devuelve el mes actual. 
 

javascript

<script type="text/javascript">
   // Creating a Date Object
   var DateObj = new Date();
 
   // Month from above Date Object is being
   // Extracted using getMonth()
   var months = DateObj.getMonth();
 
   // Printing month.
  document.write(months);
</script>

Producción: 
 

2

Navegadores compatibles: los navegadores compatibles con el método JavaScript Date getMonth() se enumeran a continuación: 
 

  • Google Chrome 1 y superior
  • Borde 12 y superior
  • Firefox 1 y superior
  • Internet Explorer 4 y superior
  • Ópera 3 y superior
  • Safari 1 y superior

Publicación traducida automáticamente

Artículo escrito por Kanchan_Ray y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *