DateTimePicker custom format ParseDateTimeValue problem

8 posts, 0 answers
  1. Predrag
    Predrag avatar
    13 posts
    Member since:
    Oct 2011

    Posted 07 Nov 2011 Link to this post

    Hi,

    I defined custom format for RadDateTimePicker “ddd dd/MM/yyyy”:

    this.radDateTimePicker.Culture = new System.Globalization.CultureInfo("de-CH");
    this.radDateTimePicker.Culture.DateTimeFormat.ShortDatePattern = "ddd dd/MM/yyyy";

    When I type date time I am receiving strange result in the ToolTip:

    10 -> 10.11.2011 – OK    

    10.10 -> 01.10.2010 – Should be 10.10.2011

    10.10.15 ->15.10.2010 – Should be 10.10.2015

    13-> 13.11.2011 – OK

    13.11 -> Error  – Should be 13.11.2011

    When I put format “dd/MM/yyyy” everuthing works fine.

    Thank you in advance for your help.

    Best regards,

    Predrag

  2. Rogier
    Rogier avatar
    3 posts
    Member since:
    Mar 2012

    Posted 05 Jul 2012 Link to this post

    I have the same issue

    Is there going to be any comments from Telerik on this thread?
  3. DevCraft banner
  4. Ivo
    Admin
    Ivo avatar
    390 posts

    Posted 06 Jul 2012 Link to this post

    Hi,

    The RadDateTimePicker's parsing logic is culture dependent - it uses the ShortDatePattern. In other words, changing the date pattern may result in different than the regular parsing. In this case I would suggest you to use the custom parsing feature provided by the RadDateTimePicker. You can read more about it here.

    Greetings,
    Ivo
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  5. Rogier
    Rogier avatar
    3 posts
    Member since:
    Mar 2012

    Posted 06 Jul 2012 Link to this post

    Ivo,

    I am trying to accomplish formatting by changing the culture only. But if I type '12' in the edit box it still suggests '01-12-2012' What am I missing. I really don't want to implement parsing myself, because I have the feeling that the control should take of this.
    public MainPage()
    {
        this.InitializeComponent();
        var culture = new CultureInfo("nl-NL")
        {
            DateTimeFormat = new DateTimeFormatInfo
            {
                ShortDatePattern = "MM.dd.yyyy",
                LongDatePattern = "MM.dd.yyyy"
            }
        };
        this.datePicker.Culture = culture;
    }
  6. Ivo
    Admin
    Ivo avatar
    390 posts

    Posted 11 Jul 2012 Link to this post

    Hi Rogier,

    I understand that implementing custom parsing logic requires some time, but I don't think that the built in parsing logic can handle all the possible scenarios. We tried to write generic parsing logic that handles as much scenarios as possible, but it seems that it doesn't work for your scenario. The great thing about writing custom parsing is that it doesn't break the RadDatePicker's built in logic for parsing.

    Greetings,
    Ivo
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  7. Juliana
    Juliana avatar
    71 posts
    Member since:
    Feb 2008

    Posted 17 Jan 2013 Link to this post

    Hi,
    I have the same issue, have you managed to accomplish custom parser?
    Thanks,
    Juliana
  8. Rogier
    Rogier avatar
    3 posts
    Member since:
    Mar 2012

    Posted 18 Jan 2013 Link to this post

    Yes we have,

    Here is our code. It's probably not the cleanest solution but it works for us.
    We implemented a handler for the ParseDateTimeValue event as below
    /// <summary>
    /// Handles the ParseDateTimeValue event of the RadDateTimePicker control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="args">The <see cref="Telerik.Windows.Controls.ParseDateTimeEventArgs"/> instance containing the event data.</param>
    private void OnParseDateTimeValue(object sender, ParseDateTimeEventArgs args)
    {
        // Always set to true... We will set it to false when needed
        args.IsParsingSuccessful = true;
     
        if (!string.IsNullOrWhiteSpace(args.TextToParse))
        {
            var s = args.TextToParse;
            if (s.IsNumeric() && s.Length >= 3)
            {
                s = string.Format("{0}{1}{2}", s.Substring(0, 2), this.dateSeparator, s.Substring(2, s.Length - 2));
                if (s.Length >= 5)
                {
                    s = string.Format("{0}{1}{2}", s.Substring(0, 5), this.dateSeparator, s.Substring(5, s.Length - 5));
                }
            }
     
            var date = this.GetDate(s);
            if (date.HasValue)
            {
                if (this.SelectableDateStart == null && this.SelectableDateEnd == null)
                {
                    // There are no restrictions on start or end date, just set the data
                    args.Result = date;
                }
                else
                {
                    if (this.SelectableDateStart != null && this.SelectableDateEnd != null && (date >= this.SelectableDateStart && date <= this.SelectableDateEnd))
                    {
                        // Start and end date are restriced, check for both
                        args.Result = date;
                    }
                    else if (this.SelectableDateStart != null && date >= this.SelectableDateStart)
                    {
                        // Only start date is restricted check for this
                        args.Result = date;
                    }
                    else if (date <= this.SelectableDateEnd)
                    {
                        // Only end date is restricted check for this
                        args.Result = date;
                    }
                }
            }
            args.IsParsingSuccessful = date != DateTime.MinValue;
        }
    }

    And then the get date method (used in eventhanler) like so:
    /// <summary>
    /// Gets the date.
    /// </summary>
    /// <param name="dateString">The date string.</param>
    /// <returns>The formatted date</returns>
    private DateTime? GetDate(string dateString)
    {
        int year = this.configurationService.PropertyConfiguration.SystemDate.Year;
        int month = this.configurationService.PropertyConfiguration.SystemDate.Month;
        int day = this.configurationService.PropertyConfiguration.SystemDate.Day;
     
        var dateArray = dateString.Split(this.dateSeparator);
        if (dateArray.Length >= 1 && dateArray[0].Length == 2 && IsAlphaNumeric(dateArray[0]))
        {
            if (this.dateParts[0].ToLower().Contains("d"))
            {
                day = int.Parse(dateArray[0]);
            }
            else if (this.dateParts[0].ToLower().Contains("m"))
            {
                month = int.Parse(dateArray[0]);
            }
            else if (this.dateParts[0].ToLower().Contains("y"))
            {
                year = int.Parse(dateArray[0]);
            }
        }
        if (dateArray.Length >= 2 && dateArray[1].Length == 2 && IsAlphaNumeric(dateArray[1]))
        {
            if (this.dateParts[1].ToLower().Contains("d"))
            {
                day = int.Parse(dateArray[1]);
            }
            else if (this.dateParts[1].ToLower().Contains("m"))
            {
                month = int.Parse(dateArray[1]);
            }
            else if (this.dateParts[1].ToLower().Contains("y"))
            {
                year = int.Parse(dateArray[1]);
            }
        }
        if (dateArray.Length == 3 && dateArray[2].Length == 4 && IsAlphaNumeric(dateArray[2]))
        {
            if (this.dateParts[2].ToLower().Contains("d"))
            {
                day = int.Parse(dateArray[2]);
            }
            else if (this.dateParts[2].ToLower().Contains("m"))
            {
                month = int.Parse(dateArray[2]);
            }
            else if (this.dateParts[2].ToLower().Contains("y"))
            {
                year = int.Parse(dateArray[2]);
            }
        }
     
        DateTime? date;
        try
        {
            date = new DateTime(year, month, day);
        }
        catch
        {
            date = null;
        }
     
        return date;
    }

    Let me know if I missed anything.

    Regards
    Rogier
  9. Juliana
    Juliana avatar
    71 posts
    Member since:
    Feb 2008

    Posted 22 Jan 2013 Link to this post

    Hi Rogier,

    Thanks for sharing your solution. You saved my day :) I wrote custom parsing based on your sample. And it seems working.
    But today I got another one. In case you are interested http://www.telerik.com/community/forums/silverlight/datepicker/displaying-only-day-and-month.aspx#2457062

    Thanks,
    Juliana
Back to Top
DevCraft banner