开发者

Remove time from a date/time string

I开发者_StackOverflow社区 have date and time stored in my database and I don't want to display both, just the date itself. When I store the date/time in a variable how do I output just the date in C#?


This is very useful:

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

In your case I would say:

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();


If you just want the date portion of a System.DateTime structure, you can use the Date property (System.DateTime.Date). It strips out hours, minutes, seconds and milliseconds.

So, if your database column datatype is defined as datetime or similar (which is the general recommandation if your database supports it), you don't have to use string and string formats.


It kind of depends where you are writing it to. The format specifier is either "{0:d}" or "{0:D}". But it depends if you are using ToString(), ToShortDateString(), ToLongDateString(), some sort of grid control, or something else altogether.


Use the provided method ToShortDateString().

eg. dateToDisplay.ToShortDateString()

Example

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


I assume that you have a variable of the type DateTime.

If you want to convert it to a string, use:

dtVar.ToShortDateString();

If you need a format info for let's say a .NET control (like a DataGrid), use:

DataFormatString="{0:d}"

Both remove the time portion of the DateTime data and use the current culture setting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜