Now Propiedad
Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

DateTime.Now (Propiedad)

 

Obtiene un objeto DateTime que se establece en la fecha y hora actual de este equipo, expresada como hora local.

Espacio de nombres:   System
Ensamblado:  mscorlib (en mscorlib.dll)

public static DateTime Now { get; }

Valor de propiedad

Type: System.DateTime

Objeto cuyo valor es la fecha y hora local.

The P:System.DateTime.Now property returns a T:System.DateTime value that represents the current date and time on the local computer. Note that there is a difference between a T:System.DateTime value, which represents the number of ticks that have elapsed since midnight of January 1, 0000, and the string representation of that T:System.DateTime value, which expresses a date and time value in a culture-specific-specific format. For information on formatting date and time values, see the M:System.DateTime.ToStringmethod. The following example displays the short date and time string in a number of culture-specific formats.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      DateTime localDate = DateTime.Now;
      String[] cultureNames = { "en-US", "en-GB", "fr-FR",
                                "de-DE", "ru-RU" };

      foreach (var cultureName in cultureNames) {
         var culture = new CultureInfo(cultureName);
         Console.WriteLine("{0}: {1}", cultureName,
                           localDate.ToString(culture));
      }
   }
}
// The example displays the following output:
//       en-US: 6/19/2015 10:03:06 AM
//       en-GB: 19/06/2015 10:03:06
//       fr-FR: 19/06/2015 10:03:06
//       de-DE: 19.06.2015 10:03:06
//       ru-RU: 19.06.2015 10:03:06

The resolution of this property depends on the system timer, which is approximately 15 milliseconds on Windows systems.As a result, repeated calls to the P:System.DateTime.Now property in a short time interval, such as in a loop, may return the same value.

The P:System.DateTime.Now property is frequently used to measure performance. However, because of its low resolution, it is not suitable for use as a benchmarking tool. A better alternative is to use the T:System.Diagnostics.Stopwatch class.

Starting with the .NET Framework version 2.0, the return value is a T:System.DateTime whose P:System.DateTime.Kind property returns F:System.DateTimeKind.Local.

System_CAPS_noteNota

You can also use the P:System.DateTimeOffset.Now property to retrieve the current local date and time. It allows a local time to be expressed unambiguously as a single point in time, which in turn makes that time value portable across computers.

The following example uses the P:System.DateTime.Now and P:System.DateTime.UtcNow properties to retrieve the current local date and time and the current universal coordinated (UTC) date and time. It then uses the formatting conventions of a number of cultures to display the strings, along with the values of the their P:System.DateTime.Kind properties.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      DateTime localDate = DateTime.Now;
      DateTime utcDate = DateTime.UtcNow;
      String[] cultureNames = { "en-US", "en-GB", "fr-FR",
                                "de-DE", "ru-RU" } ;

      foreach (var cultureName in cultureNames) {
         var culture = new CultureInfo(cultureName);
         Console.WriteLine("{0}:", culture.NativeName);
         Console.WriteLine("   Local date and time: {0}, {1:G}",
                           localDate.ToString(culture), localDate.Kind);
         Console.WriteLine("   UTC date and time: {0}, {1:G}\n",
                           utcDate.ToString(culture), utcDate.Kind);
      }
   }
}
// The example displays the following output:
//       English (United States):
//          Local date and time: 6/19/2015 10:35:50 AM, Local
//          UTC date and time: 6/19/2015 5:35:50 PM, Utc
//
//       English (United Kingdom):
//          Local date and time: 19/06/2015 10:35:50, Local
//          UTC date and time: 19/06/2015 17:35:50, Utc
//
//       français (France):
//          Local date and time: 19/06/2015 10:35:50, Local
//          UTC date and time: 19/06/2015 17:35:50, Utc
//
//       Deutsch (Deutschland):
//          Local date and time: 19.06.2015 10:35:50, Local
//          UTC date and time: 19.06.2015 17:35:50, Utc
//
//       русский (Россия):
//          Local date and time: 19.06.2015 10:35:50, Local
//          UTC date and time: 19.06.2015 17:35:50, Utc

Universal Windows Platform
Disponible desde 4.5
.NET Framework
Disponible desde 1.1
Portable Class Library
Compatible con: portable .NET platforms
Silverlight
Disponible desde 2.0
Windows Phone Silverlight
Disponible desde 7.0
Windows Phone
Disponible desde 8.1
Volver al principio
Mostrar:
© 2016 Microsoft