DateTime.IsLeapYear(Int32) Método

Definición

Devuelve una indicación en la que se precisa si el año especificado es bisiesto.

public:
 static bool IsLeapYear(int year);
public static bool IsLeapYear (int year);
static member IsLeapYear : int -> bool
Public Shared Function IsLeapYear (year As Integer) As Boolean

Parámetros

year
Int32

Año de 4 dígitos.

Devoluciones

Boolean

true si year es un año bisiesto; en caso contrario, false.

Excepciones

year es menor que 1 o mayor que 9999.

Ejemplos

En el ejemplo siguiente se usa el IsLeapYear método para determinar qué años entre 1994 y 2014 son años bisiestos. En el ejemplo también se muestra el resultado cuando se usa el AddYears método para agregar un año a un día bisiesto.

using System;

public class IsLeapYear
{
   public static void Main()
   {
      for (int year = 1994; year <= 2014; year++)
      {
         if (DateTime.IsLeapYear(year))
         {
            Console.WriteLine("{0} is a leap year.", year);
            DateTime leapDay = new DateTime(year, 2, 29);
            DateTime nextYear = leapDay.AddYears(1);
            Console.WriteLine("   One year from {0} is {1}.",
                              leapDay.ToString("d"),
                              nextYear.ToString("d"));
         }
      }
   }
}
// The example produces the following output:
//       1996 is a leap year.
//          One year from 2/29/1996 is 2/28/1997.
//       2000 is a leap year.
//          One year from 2/29/2000 is 2/28/2001.
//       2004 is a leap year.
//          One year from 2/29/2004 is 2/28/2005.
//       2008 is a leap year.
//          One year from 2/29/2008 is 2/28/2009.
//       2012 is a leap year.
//          One year from 2/29/2012 is 2/28/2013.
open System

[ 1994..2014 ]
|> List.filter DateTime.IsLeapYear
|> List.iter (fun year ->
    printfn $"{year} is a leap year."
    let leapDay = DateTime(year, 2, 29)
    let nextYear = leapDay.AddYears 1
    printfn $"   One year from {leapDay:d} is {nextYear:d}.")

// The example produces the following output:
//       1996 is a leap year.
//          One year from 2/29/1996 is 2/28/1997.
//       2000 is a leap year.
//          One year from 2/29/2000 is 2/28/2001.
//       2004 is a leap year.
//          One year from 2/29/2004 is 2/28/2005.
//       2008 is a leap year.
//          One year from 2/29/2008 is 2/28/2009.
//       2012 is a leap year.
//          One year from 2/29/2012 is 2/28/2013.
Module IsLeapYear
   Public Sub Main()
      For year As Integer = 1994 to 2014
         If DateTime.IsLeapYear(year) Then
            Console.WriteLine("{0} is a leap year.", year)
            Dim leapDay As New Date(year, 2, 29)
            Dim nextYear As Date = leapDay.AddYears(1)
            Console.WriteLine("   One year from {0} is {1}.", _
                              leapDay.ToString("d"), _
                              nextYear.ToString("d"))
         End If
      Next
   End Sub
End Module
' The example displays the following output:
'       1996 is a leap year.
'          One year from 2/29/1996 is 2/28/1997.
'       2000 is a leap year.
'          One year from 2/29/2000 is 2/28/2001.
'       2004 is a leap year.
'          One year from 2/29/2004 is 2/28/2005.
'       2008 is a leap year.
'          One year from 2/29/2008 is 2/28/2009.
'       2012 is a leap year.
'          One year from 2/29/2012 is 2/28/2013.

Comentarios

year se especifica como un número base 10 de 4 dígitos; por ejemplo, 1996.

year siempre se interpreta como un año en el calendario gregoriano. Para determinar si un año determinado era un año bisiesto en algún otro calendario, llame al método del objeto de IsLeapYear calendario.

Se aplica a