簡體   English   中英

從日期/時間字符串中刪除時間

[英]Remove time from a date/time string

我的數據庫中存儲了日期和時間,我不想同時顯示兩者,只顯示日期本身。 當我將日期/時間存儲在變量中時,如何在 C# 中僅輸出日期?

這非常有用:

http://www.csharp-examples.net/string-format-datetime/

在你的情況下,我會說:

DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
String.Format("{0:MM/dd/yy}", dt);  
DateTime dt = DateTime.Now;
...=dt.ToLongDateString();
...=dt.ToShortDateString();

如果您只需要System.DateTime結構的日期部分,則可以使用Date屬性 ( System.DateTime.Date )。 它去掉了小時、分鍾、秒和毫秒。

因此,如果您的數據庫列數據類型定義為 datetime 或類似的(如果您的數據庫支持,這是一般建議),您不必使用字符串和字符串格式。

這有點取決於您將其寫入何處。 格式說明符是“{0:d}”或“{0:D}”。 但這取決於您使用的是 ToString()、ToShortDateString()、ToLongDateString()、某種網格控件還是其他的東西。

使用提供的方法ToShortDateString()

例如。 dateToDisplay.ToShortDateString()

例子

using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      DateTime dateToDisplay = new DateTime(2009, 6, 1, 8, 42, 50);
      CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;
      // Change culture to en-US.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
      Console.WriteLine("Displaying short date for {0} culture:", 
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("   {0} (Short Date String)", 
                        dateToDisplay.ToShortDateString());
      // Display using 'd' standard format specifier to illustrate it is
      // identical to the string returned by ToShortDateString.
      Console.WriteLine("   {0} ('d' standard format specifier)", 
                        dateToDisplay.ToString("d"));
      Console.WriteLine();

      // Change culture to fr-FR.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
      Console.WriteLine("Displaying short date for {0} culture:", 
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("   {0}", dateToDisplay.ToShortDateString());
      Console.WriteLine();

      // Change culture to nl-NL.    
      Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL");
      Console.WriteLine("Displaying short date for {0} culture:", 
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("   {0}", dateToDisplay.ToShortDateString());

      // Restore original culture.
      Thread.CurrentThread.CurrentCulture = originalCulture;
   }
}
// The example displays the following output:
//       Displaying short date for en-US culture:
//          6/1/2009 (Short Date String)
//          6/1/2009 ('d' standard format specifier)
//       
//       Displaying short date for fr-FR culture:
//          01/06/2009
//       
//       Displaying short date for nl-NL culture:
//          1-6-2009

我假設您有一個DateTime類型的變量。

如果要將其轉換為字符串,請使用:

dtVar.ToShortDateString();

如果你需要一個 .NET 控件(如 DataGrid)的格式信息,請使用:

DataFormatString="{0:d}"

兩者都刪除 DateTime 數據的時間部分並使用當前區域性設置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM