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

RadMaskedDateTimeInput Edit Mode Displays Current Time Only

8 Answers 284 Views
MaskedInput (Numeric, DateTime, Text, Currency)
This is a migrated thread and some comments may be shown as answers.
Jacob
Top achievements
Rank 1
Jacob asked on 11 Jun 2013, 12:44 PM
Hi Telerik,

I am using a RadMaskedDateTimeInput control in my application. It is being used in a RadGridView's CellEditTemplate edit mode and the value is bound to a TimeSpan object with the property [ Mask = HH:mm ]

When I enter edit mode in run time it seems to be displaying/bound to Today.Now and/or DateTime.Now (It is only showing the current Hour/Minutes). Is this a known bug or should I be using another InputMask control to accomplish this. I only want to see the correct time in this format 00:00 [HH:mm]

Thanks!

8 Answers, 1 is accepted

Sort by
0
Accepted
Petar Mladenov
Telerik team
answered on 13 Jun 2013, 01:55 PM
Hello Jacob,

 RadMaskedDateTimeInput uses internally DateTime? (nullable DateTime) property to store its Value.
In other words, the Value property of this control is from type DateTime? and it cannot be directly bound to TimeSpan property. At least you need a converter. Since your binding does not succeed (due to mismatch of types), the fallback value which is DateTime.Now is being used.
So our advice is to bind the Value of the DateTimeInput to property of type DateTime?. Let us know if this fits well in your scenario.

Regards,
Petar Mladenov
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Jacob
Top achievements
Rank 1
answered on 13 Jun 2013, 02:15 PM
This is exactly it. Thank you! :)
0
Ola
Top achievements
Rank 1
answered on 19 Mar 2020, 03:39 PM

I know this is an old post, but I am doing an update anyway. I have this type of binding which have solved with a converter. I think it has been working earlier, but today I noticed that I got DateTime.Now as value in my RadMaskedDateTimeInput. The binding is working except for the initial value which is set to DateTime.Now in UI.This value is not propagated to the binding, but when I edit the UI I get the right value.

I set the Duration to 20 min by default but still I only get DateTime.Now. I have tried to bind to a DateTime? property without the converter but I still only get DateTime.Now in my UI.

 

Here is the control:

<telerik:RadMaskedDateTimeInput
   x:Name="DurationInput"
   EmptyContent="{dt:Text HHMM}"
   InputBehavior="Replace"
   Mask="HH:mm"
   SelectionOnFocus="SelectAll"
   TextMode="PlainText"
   UpdateValueEvent="LostFocus"
   Value="{Binding Duration, Mode=TwoWay, Converter={StaticResource TimeSpanDateTimeConverter}}"/>

 

And this is my converter:

using System;
   using System.Globalization;
   using System.Windows.Data;
 
   public class TimeSpanDateTimeConverter : IValueConverter
   {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
         // TimeSpan in
         if (!(value is TimeSpan))
         {
            return default(DateTime?);
         }
 
         var timeSpan = (TimeSpan) value;
 
         // DateTime out
         if (targetType != typeof(DateTime?))
         {
            return null;
         }
 
         var nowDate = DateTime.Now.Date;
         return nowDate + timeSpan;
      }
 
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
         // DateTime in
         var dateTime = (DateTime?) value;
 
         // TimeSpan out
         return targetType != typeof(TimeSpan?)
            ? null
            : dateTime?.TimeOfDay;
      }
   }

 

And this is my view-model property:

public TimeSpan? Duration
{
   get => _duration;
   set
   {
      SetProperty(ref _duration, value);
   }
}

 

I am using Telerik 2020.1.115.

Best regards

Ola

 

 

0
Petar Mladenov
Telerik team
answered on 22 Mar 2020, 03:12 PM

Hi Ola,

Your converter adds 20 minutes timespan to a datetime which does not take into account the hours, minutes and seconds.           

var nowDate = DateTime.Now.Date; //- this returns date with time 12:00.
return nowDate + timeSpan; // - this returns unexpected result

Immediate window:

nowDate + timeSpan
{3/22/2020 12:20:00 AM}
    Date: {3/22/2020 12:00:00 AM}
    Day: 22
    DayOfWeek: Sunday
    DayOfYear: 82
    Hour: 0
    Kind: Local
    Millisecond: 0
    Minute: 20
    Month: 3
    Second: 0
    Ticks: 637204332000000000
    TimeOfDay: {00:20:00}
    Year: 2020

What I can suggest is using DateTime.Add method and this way add datatime.Now and the rimespan

var nowDate = DateTime.Now.Add(timeSpan);
return nowDate;
Regards,
Petar Mladenov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Ola
Top achievements
Rank 1
answered on 23 Mar 2020, 09:41 AM

I don't agree. DateTime.Now.Date returns a DateTime holding midnight today. Just the way I want it.

> Console.WriteLine(DateTime.Now.Date + TimeSpan.FromMinutes(20))
2020-03-23 00:20:00
>

 

And if I instead use a fixed DateTime pointing at midnight I get the same behavior in RadMaskedDateTimeInput. As I pointed out in my previous post the binding and converter works, but the control still displays DateTime.Now as initial value. This value is not reflected to my view-model but if I edit the value it is reflected to the view-model. Besides, I think the whole implementation was working when I first implemented it some time ago. Have you verified that you dont have any issues in RadMaskedDateTimeInput?

Best regards,

Ola

0
Petar Mladenov
Telerik team
answered on 26 Mar 2020, 05:59 AM

Hi Ola,

I first misunderstood your requirement, I thought you needed to display the hour and minutes of a time 20 minutes from now. That is why I suggested the code which uses the "Add" method.

Today I tested your code against 3 major versions - 2012 R3, 2017 R1 and 2020 R1 SP (our latest official). I get exactly the opposite of yours. In 2012 control seems broken and displays DateTime.Now initially and looks fixed in 2017 (I can precisely find the fix if needed) and in 2020 - controls shows 

"00:20" initially - I guess this is exactly what you expect. I have attached here my test project so you can check if there are any differences with yours. Please let me know if I am missing something.

Regards,
Petar Mladenov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Ola
Top achievements
Rank 1
answered on 17 Apr 2020, 11:26 AM

Alright, now I got some time to look at this again and I was able to pinpoint the problem.

In one dialog I got the RadMaskedDateTimeInput first in order, which means that the initial focus is set to this control. If I set the property UpdateValueEvent="LostFocus" on the RadMaskedDateTimeInput, the initial value is not propagated to the control. If I instead have UpdateValueEvent="PropertyChanged" or have another control first in order the value is set on the control.

Is this expected behavior?

0
Petar Mladenov
Telerik team
answered on 20 Apr 2020, 02:17 PM

Hi Ola,

Can you replicate the described behavior in an isolated sample that we can look and eventually provide you with solution ?

Currently, in the last project we sent you the initial value of the UpdateValueEvent property is LostFocus and value on load is formatted "00:20" as expected.

Regards,
Petar Mladenov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
MaskedInput (Numeric, DateTime, Text, Currency)
Asked by
Jacob
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Jacob
Top achievements
Rank 1
Ola
Top achievements
Rank 1
Share this question
or