Setting different date format in the asp.net application

we can set different date format and override the regional settings.


Check the below code


//aspx page

<asp:RadioButtonList ID="rbOption" runat="server">
            <asp:ListItem Selected="True">MM/dd/yyyy</asp:ListItem>
            <asp:ListItem>dd/MM/yyyy</asp:ListItem>
            <asp:ListItem>dd-MMM-yyyy</asp:ListItem>
</asp:RadioButtonList>

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Show Date" /><br />
        <br />
        Date : <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>



//Code behind

 protected void Button1_Click(object sender, EventArgs e)
    {
        CultureInfo CI = new CultureInfo("en-us");       
      
       CI.DateTimeFormat.ShortDatePattern = rbOption.SelectedValue.ToString();

        System.Threading.Thread.CurrentThread.CurrentCulture = CI;
      
        TextBox1.Text = DateTime.Parse("2000" + "/05/" + "28").ToShortDateString();

    }


Namespace used here is  "System.Globalization"

Sample output -

MM/dd/yyyy     - 05/28/2000
dd/MM/yyyy     - 28/05/2000
dd-MMM-yyyy - 28-May-2000

No comments:

Post a Comment

Flipkart