How to use Thailand Calendar in RadDatePicker

Thread is closed for posting
4 posts, 0 answers
  1. Marin Bratanov
    Marin Bratanov avatar
    23 posts
    Member since:
    Jul 2012

    Posted 08 Oct 2018 Link to this post

    This applies to all Telerik UI for ASP.NET AJAX calendars and date/time pickers.

    All of these controls support Gergorian calendars only and so various other calendars (like the Buddhist or Hijri) may cause issues like date displacement.

    For example, the Thailand calendar uses a Buddhist year which is offset from the Gregorian calendar by 543 years. So, for this particular culture an easy workaround would be to subtract those years:

    <telerik:RadDatePicker runat="server" ID="rdp1">
    </telerik:RadDatePicker>
    <asp:Button Text="get date on server" ID="btn1" OnClick="btn1_Click" runat="server" />
    protected override void InitializeCulture()
    {
        //set Thai culture to the page
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("th-TH");
    }
     
    protected void btn1_Click(object sender, EventArgs e)
    {
        DateTime? selDate = rdp1.SelectedDate;
        if (System.Threading.Thread.CurrentThread.CurrentCulture.Name == "th-TH" && selDate.HasValue)
        {
            //subtract the year difference from the dates
            DateTime targetTime = selDate.GetValueOrDefault().AddYears(-543);
            Response.Write(targetTime);
        }
     
    }

    An alternative that may be more generic is to set the current culture calendar to always be Gregorian:

     

    protected override void InitializeCulture()
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("th-TH");
        //always use Gregorian calendar
        System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.Calendar = new System.Globalization.GregorianCalendar();
    }

     

    You can also find an alternative approach that creates new CultureInfo classes with a Gregorian calendar based on the current culture in the following code library: How to set up r.a.d.datepicker with cultures that use default Hijri Calendar.

    NOTE: Telerik cannot guarantee these workaround will produce the desired effect, nor how they can scale across multiple cultures and calendars.

  2. Marin Bratanov
    Admin
    Marin Bratanov avatar
    5948 posts

    Posted 10 Oct 2018 Link to this post


    The .GetYear() and other methods of the .NET Calendar instance seem to be able to convert between calendars. Here is a small test you can try as well.

    Copy Code
    protected void Page_Load(object sender, EventArgs e)
    {
        //this is something similar to what happens when the date is parsed after the POST
        DateTime startYear = new DateTime(2000, 11, 22, new System.Globalization.ThaiBuddhistCalendar());
        //so such a conversion should be OK, as long as .NET does it well, I could not find proper documentation that confirms
        //that it is supposed to work, even though it seems to, and so I am not in a position to guarantee this approach is safe
        System.Globalization.Calendar gregCal = new System.Globalization.GregorianCalendar();
        Response.Write(gregCal.GetYear(startYear));
    }

     

    which prints out 1457 as expected.

    Copy Code
    protected void Page_Load(object sender, EventArgs e)
    {
        //this is even more similar to what happens when the date is parsed after the POST
        DateTime startYear = new DateTime(2000, 11, 22, System.Threading.Thread.CurrentThread.CurrentCulture.Calendar);
        //so such a conversion should be OK, as long as .NET does it well, I could not find proper documentation that confirms
        //that it is supposed to work, even though it seems to, and so I am not in a position to guarantee this approach is safe
        System.Globalization.Calendar gregCal = new System.Globalization.GregorianCalendar();
        Response.Write(gregCal.GetYear(startYear));
    }
     
    protected override void InitializeCulture()
    {
        //set Thai culture to the page
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("th-TH");
    }

     


  3. Daniel
    Daniel avatar
    9 posts
    Member since:
    Nov 2013

    Posted 11 Oct 2018 in reply to Marin Bratanov Link to this post

    I would like to add a web page and code behind that describes how to do this.  I've posted the Default.aspx and Default.aspx.cs files.  You will need to supply the rest of the solution and try it out.

  4. Marin Bratanov
    Admin
    Marin Bratanov avatar
    5948 posts

    Posted 11 Oct 2018 Link to this post

    Thank you for your contribution, Daniel, that's a thorough approach in exploring the various cultures and their calendars and formats. You will find your Telerik points updated.

    The conversion methods at the end (ConvertToDBDate called from txtRequiredDeliveryDate_SelectedDateChanged) is at the end for those who are only looking for a conversion approach.

    --Marin

Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.