This is a migrated thread and some comments may be shown as answers.

DateTimePicker custom format ParseDateTimeValue problem

7 Answers 204 Views
DatePicker
This is a migrated thread and some comments may be shown as answers.
Predrag
Top achievements
Rank 1
Predrag asked on 07 Nov 2011, 04:45 PM

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

7 Answers, 1 is accepted

Sort by
0
Rogier
Top achievements
Rank 1
answered on 05 Jul 2012, 02:54 PM
I have the same issue

Is there going to be any comments from Telerik on this thread?
0
Ivo
Telerik team
answered on 06 Jul 2012, 06:29 AM
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 >>

0
Rogier
Top achievements
Rank 1
answered on 06 Jul 2012, 11:44 AM
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;
}
0
Ivo
Telerik team
answered on 11 Jul 2012, 12:20 PM
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 >>

0
Juliana
Top achievements
Rank 1
answered on 17 Jan 2013, 02:33 PM
Hi,
I have the same issue, have you managed to accomplish custom parser?
Thanks,
Juliana
0
Rogier
Top achievements
Rank 1
answered on 18 Jan 2013, 12:56 PM
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
0
Juliana
Top achievements
Rank 1
answered on 22 Jan 2013, 11:41 AM
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
Tags
DatePicker
Asked by
Predrag
Top achievements
Rank 1
Answers by
Rogier
Top achievements
Rank 1
Ivo
Telerik team
Juliana
Top achievements
Rank 1
Share this question
or