[SOLVED] Formatting ToShortDateString to dd/MM/yyyy

Issue

I have Calendar like this one:

View

 <td>
     <asp:Calendar ID="Calendar1" runat="server" 
      OnSelectionChanged="DateChange">
     </asp:Calendar>
     <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
 </td>

.Cs file

protected void Page_Load(object sender, EventArgs e)
    {
        TextBox2.Text = DateTime.Today.ToShortDateString()+'.';
    }

    protected void DateChange(object sender, EventArgs e)
    {
        TextBox2.Text = Calendar1.SelectedDate.ToShortDateString() + '.';
    }

It display date as "MM/dd/yyyy", but I want to display it as "dd/MM/yyyy", I try it changing DateTime.Today.ToShortDateString()+'.'; to DateTime.Today.ToShortDateString("dd/MM/yyyy");

but I get

Error 3 No overload for method ‘ToShortDateString’ takes 1 arguments

What can I do to solve this?

Solution

ToShortDateString does not have an overload which takes any parameter.

If your ToShortDateString() returns MM/dd/yyyy format, that means your CurrentCulture has this format in it’s ShortDatePattern property.

You can always use custom formatting for that like with proper culture like;

TextBox2.Text = DateTime.Today.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);

Answered By – Soner Gönül

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *