DateTime.Date 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.
Mendapatkan komponen tanggal instans ini.
public:
property DateTime Date { DateTime get(); };
public DateTime Date { get; }
member this.Date : DateTime
Public ReadOnly Property Date As DateTime
Nilai Properti
Objek baru dengan tanggal yang sama dengan instans ini, dan nilai waktu diatur ke 12:00:00 tengah malam (00:00:00).
Contoh
Contoh berikut menggunakan Date properti untuk mengekstrak komponen DateTime tanggal nilai dengan komponen waktunya diatur ke nol (atau 0:00:00, atau tengah malam). Ini juga menggambarkan bahwa, tergantung pada string format yang digunakan saat menampilkan DateTime nilai, komponen waktu dapat terus muncul dalam output yang diformat.
using System;
public class Example
{
public static void Main()
{
DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
Console.WriteLine(date1.ToString());
// Get date-only portion of date, without its time.
DateTime dateOnly = date1.Date;
// Display date using short date string.
Console.WriteLine(dateOnly.ToString("d"));
// Display date using 24-hour clock.
Console.WriteLine(dateOnly.ToString("g"));
Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));
}
}
// The example displays output like the following output:
// 6/1/2008 7:47:00 AM
// 6/1/2008
// 6/1/2008 12:00 AM
// 06/01/2008 00:00
open System
let date1 = DateTime(2008, 6, 1, 7, 47, 0)
printfn $"{date1}"
// Get date-only portion of date, without its time.
let dateOnly = date1.Date
// Display date using short date string.
printfn $"{dateOnly:d}"
// Display date using 24-hour clock.
printfn $"{dateOnly:g}"
printfn $"""{dateOnly.ToString "MM/dd/yyyy HH:mm"}"""
// The example displays output like the following output:
// 6/1/2008 7:47:00 AM
// 6/1/2008
// 6/1/2008 12:00 AM
// 06/01/2008 00:00
Module Example
Public Sub Main()
Dim date1 As Date = #6/1/2008 7:47AM#
Console.WriteLine(date1.ToString())
' Get date-only portion of date, without its time.
Dim dateOnly As Date = date1.Date
' Display date using short date string.
Console.WriteLine(dateOnly.ToString("d"))
' Display date using 24-hour clock.
Console.WriteLine(dateOnly.ToString("g"))
Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"))
End Sub
End Module
' The example displays output like the following:
' 6/1/2008 7:47:00 AM
' 6/1/2008
' 6/1/2008 12:00 AM
' 06/01/2008 00:00
Keterangan
Nilai Kind properti dari nilai yang dikembalikan DateTime sama dengan instans saat ini.
Karena jenis mewakili DateTime tanggal dan waktu dalam satu jenis, penting untuk menghindari salah menafsirkan tanggal yang dikembalikan oleh Date properti sebagai tanggal dan waktu.