Time format Issue

4 posts, 0 answers
  1. Richard van Diggelen
    Richard van Diggelen avatar
    8 posts
    Member since:
    Jul 2012

    Posted 31 Mar 2011 Link to this post

    Hi,

    I had found an issue with WPF TimePicker control for .Net 4.0.
    When I type in 130 in the time field, it gets converted to 1PM. Its coz it considers it as 13 and 0.So it becomes 1:00PM instead of 1:30AM.
    Similary 230 converts to 11PM, thinking its 23:00hrs.
    This issue works fine in ASP.Net control (ie:130 gets converted to 1:30AM). I had tried with the demo version of the latest release.

    Is this a bug?

    Thanks
  2. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 05 Apr 2011 Link to this post

    Hello Richard van Diggelen,

    This is the default parser behavior and it is not possible for a customization. A possible solution is to use the ParseDateTime event, where you can specify your own result. Let us know if you need any further information.


    All the best,
    Kaloyan
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. Tore
    Tore avatar
    4 posts
    Member since:
    Mar 2011

    Posted 23 Mar 2012 Link to this post

    I have found a way to fix the RadTimePicker using WPF styling and event setters. First off, add a ResourceDictionary (ControlStyles.xaml): 

    <ResourceDictionary x:Class="EventSetterWPF.ControlStyles"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tc="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input">


    <Style TargetType="{x:Type tc:RadTimePicker}">
    <EventSetter Event="ParseDateTimeValue" Handler="RadTimePicker_ParseDateTimeValue" />
    </Style>

    </ResourceDictionary>

    Next off, add a codebehind for the ResourceDictionary (ControlStyles.xaml.cs)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using Telerik.Windows.Controls;

    namespace EventSetterWPF
    {


    public partial class ControlStyles : ResourceDictionary
    {

    public ControlStyles()
    {
    InitializeComponent();
    }

    public void RadTimePicker_ParseDateTimeValue(object sender, ParseDateTimeEventArgs e)
    {
    if (e.IsParsingSuccessful)
    {
    string originalString = e.TextToParse.Replace(":", "");
    if (!string.IsNullOrEmpty(originalString) && originalString.Length == 4 && originalString.StartsWith("0"))
    {
    int enteredHour = -1;
    int enteredMinutes = -1;
    if (int.TryParse(originalString.Substring(0, 2), out enteredHour) &&
    (int.TryParse(originalString.Substring(2, 2), out enteredMinutes)) &&
    e.Result.HasValue)
    {
    DateTime originalValue = e.Result.Value;
    DateTime? newValue = new DateTime(originalValue.Year, originalValue.Month, originalValue.Day,
    enteredHour, enteredMinutes, 0);
    e.Result = newValue;
    }
    }
    }
    }

    }

    }

    Add the resource dictionary to App.xaml: 

    <Application x:Class="EventSetterWPF.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="TestRadTimePicker.xaml">
        <Application.Resources>
            <ResourceDictionary Source="ControlStyles.xaml" /> 
        </Application.Resources>
    </Application>
    

    Now, all your RadTimePicker will have this custom datetime parsing. Similar strategy can be used for RadDatePicker and RadDateTimePicker. I had to fix these issues at work today for multiple Telerik Controls. 



    For example, typing "0130" did not parse to 01:30 AM, rather it resulted in 13:00 (PM) being set. Using the strategy above made the parsing of the RadTimePicker work again. 

    I included a blog article for the interested reader here:  

    http://toreaurstad.blogspot.com/2012/03/styling-user-controls-in-wpf-with.html 

  5. Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 29 Mar 2012 Link to this post

    Hello Tore,

    Thank you for sharing your solution with us. It would be very helpful to all encountered the same problem.

    All the best,
    Konstantina
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top