Want Date Format in dd/mm/yyyy in ASP.Net With C#. - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Want Date Format in dd/mm/yyyy in ASP.Net With C#.

frmsaspfrmsasp Posts: 12Member
Dear Friend,

I am new in ASP.Net with c#.

I want date in a dd/mm/yyyy format with validation in TextBox . Is anybody guide me how shall ?

As I got date with dd/mm/yyyy but it will also accept 32/02/2005. Though Feb has less 30 days.

Also when I compare two textbox it will not evalute properly. eg I want TextBox2 date greater then TextBox1. So I had used
CompareValidator, & select type="date" but it will not evalute.

Pl guide me.


Thanking You,





Comments

  • iwilld0itiwilld0it Posts: 1,134Member
    There is many ways to do it. You can use a custom format string or you can let your computers current culture determine the format.

    Method 1
    [code]
    DateTime dt = DateTime.Now;

    string sDate = dt.ToString("dd/MM/yyyy");
    [/code]

    This is culture unaware. The format string can be layed out anyway you like.

    Method 2
    [code]
    using System.Globalization;

    DateTime dt = DateTime.Now;

    string sDate = dt.ToString("d", new CultureInfo("en-CA"))
    [/code]

    This is a Culture aware example. It takes advantage of the fact that Candians format their dates as dd/MM/yyyy. The [b]en-CA[/b] is a culture string. Each country has a different code. To see a list of codes for each country, use this code:

    [code]
    foreach(CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
    {
    Response.Write(ci.EnglishName + " = " + ci.ToString() + "
    ");
    }
    [/code]

    Method 3
    [code]
    using System.Globalization;

    DateTime dt = DateTime.Now;

    string sDate = dt.ToString("d", CultureInfo.CurrentUICulture);
    [/code]

    This method assumes that your computers current culture will format the date the way your country typically formats it. So if your from Candana for instance, then your current culture will most-likely be
    en-CA and the date will be formatted as dd/MM/yyyy.

    Method 4
    [code]
    DateTime dt = DateTime.Now;

    string sDate = dt.ToShortDateString();
    [/code]

    This method makes the same assumption as Method 3, accept it uses a built in DateTime function that simplifies it.

    Hope this helps.
  • avicool08avicool08 Posts: 2Member
    [color=Blue][b]THIS WORKS THANKS[/b][/color]

    : There is many ways to do it. You can use a custom format string or
    : you can let your computers current culture determine the format.
    :
    : Method 1
    : [code]:
    : DateTime dt = DateTime.Now;
    :
    : string sDate = dt.ToString("dd/MM/yyyy");
    : [/code]:
    :
    : This is culture unaware. The format string can be layed out anyway
    : you like.
    :
    : Method 2
    : [code]:
    : using System.Globalization;
    :
    : DateTime dt = DateTime.Now;
    :
    : string sDate = dt.ToString("d", new CultureInfo("en-CA"))
    : [/code]:
    :
    : This is a Culture aware example. It takes advantage of the fact that
    : Candians format their dates as dd/MM/yyyy. The [b]en-CA[/b] is a
    : culture string. Each country has a different code. To see a list of
    : codes for each country, use this code:
    :
    : [code]:
    : foreach(CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
    : {
    : Response.Write(ci.EnglishName + " = " + ci.ToString() + "
    ");
    : }
    : [/code]:
    :
    : Method 3
    : [code]:
    : using System.Globalization;
    :
    : DateTime dt = DateTime.Now;
    :
    : string sDate = dt.ToString("d", CultureInfo.CurrentUICulture);
    : [/code]:
    :
    : This method assumes that your computers current culture will format
    : the date the way your country typically formats it. So if your from
    : Candana for instance, then your current culture will most-likely be
    : en-CA and the date will be formatted as dd/MM/yyyy.
    :
    : Method 4
    : [code]:
    : DateTime dt = DateTime.Now;
    :
    : string sDate = dt.ToShortDateString();
    : [/code]:
    :
    : This method makes the same assumption as Method 3, accept it uses a
    : built in DateTime function that simplifies it.
    :
    : Hope this helps.
    :

Sign In or Register to comment.