I want my datetime to be converted to a string that is in format "dd/MM/yyyy"
Whenever I convert it using DateTime.ToString("dd/MM/yyyy")
, I get dd-MM-yyyy
instead.
Is there some sort of culture info that I have to set?
I want my datetime to be converted to a string that is in format "dd/MM/yyyy"
Whenever I convert it using DateTime.ToString("dd/MM/yyyy")
, I get dd-MM-yyyy
instead.
Is there some sort of culture info that I have to set?
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")
/
:)
Commented
Mar 7, 2014 at 8:46
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>
Try this.
var DateFormat = "dd-MMM-yy"; var NumFormat = "###,###,##0.00"
@foreach (DataRow _Row in CashBook2.Rows)
{
<tr>
<td>@DateTime.Parse(_Row["Vou_Date"].ToString()).ToString(DateFormat)</td>
<td>@_Row["Vou_No"].ToString()</td>
<td>@_Row["Description"].ToString()</td>
<td>@decimal.Parse(_Row["DR"].ToString()).ToString(NumFormat)</td>
<td>@decimal.Parse(_Row["CR"].ToString()).ToString(NumFormat)</td>
<td>@Balance</td>
</tr>
}