Compartir a través de


DateTime.Now Propiedad

Definición

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

public:
 static property DateTime Now { DateTime get(); };
public static DateTime Now { get; }
static member Now : DateTime
Public Shared ReadOnly Property Now As DateTime

Valor de propiedad

Objeto cuyo valor es la fecha y hora local.

Ejemplos

En el ejemplo siguiente se usan las Now propiedades y UtcNow para recuperar la fecha y hora locales actuales y la fecha y hora universal coordinadas (UTC) actuales. A continuación, usa las convenciones de formato de una serie de referencias culturales para mostrar las cadenas, junto con los valores de sus Kind propiedades.

using namespace System;
using namespace System::Globalization;

void main()
{
   DateTime localDate = DateTime::Now;
   DateTime utcDate = DateTime::UtcNow;
   array<String^>^ cultureNames = { "en-US", "en-GB", "fr-FR",
                                    "de-DE", "ru-RU" } ;

   for each (String^ cultureName in cultureNames) {
      CultureInfo^ culture = gcnew 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
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
open System
open System.Globalization

let localDate = DateTime.Now
let utcDate = DateTime.UtcNow
let cultureNames = 
    [ "en-US"; "en-GB"; "fr-FR"; "de-DE"; "ru-RU" ] 

for cultureName in cultureNames do
    let culture = CultureInfo cultureName
    printfn $"{culture.NativeName}:"
    printfn $"   Local date and time: {localDate.ToString culture}, {localDate.Kind:G}"
    printfn $"   UTC date and time: {utcDate.ToString culture}, {utcDate.Kind:G}\n"

// 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
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim localDate = DateTime.Now
      Dim utcDate = DateTime.UtcNow
      Dim cultureNames() As String = { "en-US", "en-GB", "fr-FR",
                                       "de-DE", "ru-RU" }

      For Each cultureName In cultureNames
         Dim culture As 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}",
                           utcDate.ToString(culture), utcDate.Kind)
         Console.WriteLine()
      Next
   End Sub
End Module
' 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

Comentarios

La Now propiedad devuelve un DateTime valor que representa la fecha y hora actuales en el equipo local. Tenga en cuenta que hay una diferencia entre un DateTime valor, que representa el número de tics transcurridos desde la medianoche del 1 de enero de 0001 y la representación de cadena de ese DateTime valor, que expresa un valor de fecha y hora en un formato específico de la referencia cultural. Para obtener información sobre el formato de los valores de fecha y hora, consulte el ToString método . En el ejemplo siguiente se muestra la cadena de fecha y hora corta en varios formatos específicos de la referencia cultural.

using namespace System;
using namespace System::Globalization;

void main()
{
   DateTime localDate = DateTime::Now;
   array<String^>^ cultureNames = { "en-US", "en-GB", "fr-FR",
                                    "de-DE", "ru-RU" };

   for each (String^ cultureName in cultureNames) {
      CultureInfo^ culture = gcnew 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
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
open System
open System.Globalization

let localDate = DateTime.Now
let cultureNames = 
    [ "en-US"; "en-GB"; "fr-FR"; "de-DE"; "ru-RU" ]

for cultureName in cultureNames do
    let culture = CultureInfo cultureName
    printfn $"{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
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim localDate = DateTime.Now
      Dim cultureNames() As String = { "en-US", "en-GB", "fr-FR",
                                       "de-DE", "ru-RU" }

      For Each cultureName In cultureNames
         Dim culture As New CultureInfo(cultureName)
         Console.WriteLine("{0}: {1}", cultureName,
                           localDate.ToString(culture))
      Next
   End Sub
End Module
' 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

La resolución de esta propiedad depende del temporizador del sistema, que depende del sistema operativo subyacente. Tiende a estar entre 0,5 y 15 milisegundos. Como resultado, las llamadas repetidas a la Now propiedad en un intervalo de tiempo corto, como en un bucle, pueden devolver el mismo valor.

La Now propiedad se usa con frecuencia para medir el rendimiento. Sin embargo, debido a su baja resolución, no es adecuado para su uso como herramienta de pruebas comparativas. Una alternativa mejor es usar la Stopwatch clase .

A partir de .NET Framework versión 2.0, el valor devuelto es una DateTime cuya Kind propiedad devuelve DateTimeKind.Local.

Nota

También puede usar la DateTimeOffset.Now propiedad para recuperar la fecha y hora locales actuales. Permite expresar una hora local de forma inequívoca como un único punto en el tiempo, lo que, a su vez, hace que ese valor de hora sea portátil entre los equipos.

Se aplica a

Consulte también