DateTime.Now Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
DateTime Mendapatkan objek yang diatur ke tanggal dan waktu saat ini pada komputer ini, yang dinyatakan sebagai waktu lokal.
public:
static property DateTime Now { DateTime get(); };
public static DateTime Now { get; }
static member Now : DateTime
Public Shared ReadOnly Property Now As DateTime
Nilai Properti
Objek yang nilainya adalah tanggal dan waktu lokal saat ini.
Contoh
Contoh berikut menggunakan Now properti dan UtcNow untuk mengambil tanggal dan waktu lokal saat ini dan tanggal dan waktu terkoordinasi universal (UTC) saat ini. Kemudian menggunakan konvensi pemformatan sejumlah budaya untuk menampilkan string, bersama dengan nilai propertinya Kind .
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
Keterangan
Properti Now mengembalikan DateTime nilai yang menunjukkan tanggal dan waktu saat ini di komputer lokal. Perhatikan bahwa ada perbedaan antara DateTime nilai, yang mewakili jumlah kutu yang telah berlalu sejak tengah malam 1 Januari 0001, dan representasi string dari DateTime nilai tersebut, yang mengekspresikan nilai tanggal dan waktu dalam format khusus budaya. Untuk informasi tentang memformat nilai tanggal dan waktu, lihat ToString metode . Contoh berikut menampilkan string tanggal dan waktu singkat dalam sejumlah format khusus budaya.
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
Resolusi properti ini tergantung pada timer sistem, yang tergantung pada sistem operasi yang mendasar. Ini cenderung antara 0,5 dan 15 milidetik. Akibatnya, panggilan berulang ke Now properti dalam interval waktu singkat, seperti dalam perulangan, dapat mengembalikan nilai yang sama.
Properti Now sering digunakan untuk mengukur performa. Namun, karena resolusinya yang rendah, tidak cocok untuk digunakan sebagai alat tolok ukur. Alternatif yang lebih baik adalah menggunakan Stopwatch kelas .
Dimulai dengan .NET Framework versi 2.0, nilai yang dikembalikan adalah properti DateTime yang Kind mengembalikan DateTimeKind.Local.
Nota
Anda juga dapat menggunakan DateTimeOffset.Now properti untuk mengambil tanggal dan waktu lokal saat ini. Ini memungkinkan waktu lokal untuk diekspresikan secara tidak ambigu sebagai satu titik waktu, yang pada gilirannya membuat nilai waktu tersebut portabel di seluruh komputer.