Remove time from date / time string

I have the date and time stored in my database, and I do not want to display them, only the date itself. When I store the date / time in a variable, how do I only output the date in C #?

+8
c # datetime
source share
6 answers

It 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); 
+9
source share
 DateTime dt = DateTime.Now; ...=dt.ToLongDateString(); ...=dt.ToShortDateString(); 
+9
source share

If you only need the date part of the System.DateTime structure, you can use the Date property ( System.DateTime.Date ). It removes hours, minutes, seconds and milliseconds.

So, if your database column data type is defined as a date or time (which is a general recommendation if your database supports it), you do not need to use string and string formats.

+9
source share

It depends on where you write it. The format specifier is "{0: d}" or "{0: D}". But it depends on whether you use ToString (), ToShortDateString (), ToLongDateString (), some kind of grid control or something else at all.

+4
source share

Use the provided ToShortDateString () method.

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 
+1
source share

I assume you have a variable of type DateTime .

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

 dtVar.ToShortDateString(); 

If you need format information for let, for example, a .NET control (e.g. DataGrid), use:

 DataFormatString="{0:d}" 

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

+1
source share

Source: https://habr.com/ru/post/649841/


All Articles