I need to duplicate Teleriks time parsing logic

2 posts, 0 answers
  1. Jason
    Jason avatar
    30 posts
    Member since:
    May 2009

    Posted 06 Jul 2011 Link to this post

    Hi, due to the fact that slashes are required for date time entry (which i agree with others that its a pain to require it) I had to implement my own parsing logic by overriding OnParseDateTime.

    By doing this we were able to support our custom date formats that do not require slashes but now we need to be able to type in the time formats as they exist in the control. 

    We have determined with the way we are parsing the string we would need to come up with about 100 different formats to cover all of the formats that the control supports by default with its default parsing.

    Is there any way to get the existing parsing logic built into the control so i can utilize the same time parsing portion of it?
  2. Pana
    Pana avatar
    748 posts

    Posted 14 Jul 2011 Link to this post

    Hello Jason,

    Can not get easier than that:

    Extend the RadDateTimePicker. Override its virutal parsing method:
    public class CustomDateTimePicker : RadDateTimePicker
        protected override void OnParseDateTime(ParseDateTimeEventArgs e)
            DateTime dt = new DateTime();
            bool isSuccessful = false;
            ParseDateTimeEventArgs pdtea = new ParseDateTimeEventArgs(e.RoutedEvent, e.TextToParse.Replace(".", "/"), e.PreviousValue, dt, isSuccessful);
            pdtea.Result = pdtea.Result;

    Replace all the "." with "/", create new fake ParseDateTimeEventArgs and use the base.OnParseDateTime to do the built in parsing for you.

    Use the new CustomDateTimePicker like that:
    <UserControl x:Class="DateTimePickerCustomParsing.MainPage"
        <Grid x:Name="LayoutRoot" Background="White">
            <local:CustomDateTimePicker x:Name="DTP" Width="150" Height="22" />

    And the DateTimePicker will parse strings like 1.1.2001 to a date like 1/1/2001. The only problem is that once the date is parsed from string to DateTime other methods are used to render the DateTime back to string and "/" will appear on the place of the ".". You can change the DateTimePicker's culture in order to use . as separators but then "/" will be lost and converted to ".".

    Best wishes,
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

  3. DevCraft R3 2016 release webinar banner
Back to Top