To do this, you use the "MM/yyyy" format string. The format string uses the current culture's date separator. Getting a string that contains the date and time in a specific format. For example, the "MM/dd/yyyyHH:mm" format string displays the date and time string in a fixed format such as "19//03//2013 18:06".
DateTimeFormat. ShortDatePattern = "dd-MMM-yyyy"; culture.
ToString() − One more way to get the date from DateTime is using ToString() extension method. The advantage of using ToString() extension method is that we can specify the format of the date that we want to fetch. DateTime. Date − will also remove the time from the DateTime and provides us the Date only.
Slash is a date delimiter, so that will use the current culture date delimiter.
If you want to hard-code it to always use slash, you can do something like this:
DateTime.ToString("dd'/'MM'/'yyyy")
Pass CultureInfo.InvariantCulture as the second parameter of DateTime, it will return the string as what you want, even a very special format:
DateTime.Now.ToString("dd|MM|yyyy", CultureInfo.InvariantCulture)
will return: 28|02|2014
Add CultureInfo.InvariantCulture
as an argument:
using System.Globalization;
...
var dateTime = new DateTime(2016,8,16);
dateTime.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
Will return:
"16/08/2016"
If you use MVC, tables, it works like this:
<td>@(((DateTime)detalle.fec).ToString("dd'/'MM'/'yyyy"))</td>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With