This is a migrated thread and some comments may be shown as answers.
TimePicker Defaults
5 Answers 58 Views
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 24 Oct 2014, 06:13 PM
I'm writing a time entry program that will require a lot of time pickers on a single form. I have to do some math to add up the hours based on TimeIn and TimeOut for each shift.

I set the control to a default date and time on load. The trouble I'm having is when the user changes a time using the picker, the date reverts to the current date. The result is my date math is wrong. If the TimeIn date and time is set to 10/21/2014 1:00 AM on load and they change the time to 2:00 AM, I'm getting 10/24/2014 2:00 AM, which is a problem for my math.

1) Is there are way to format the radtimepicker to be time only and return a time only value? I don't care about the date portion at all. I'm just using these to get hours worked in a single day.

2) Alternatively, since I'm stashing the times in a database, is there someway to keep the date from changing? I can work with a fixed date for time calculations.

Thanks

5 Answers, 1 is accepted

Sort by
0
Chris
Top achievements
Rank 1
answered on 24 Oct 2014, 07:04 PM
What seems to be happening is when you use the up/down arrows to adjust the time, the date changes to today's date, even though the date portion is not seen on the user end.

0
Chris
Top achievements
Rank 1
answered on 25 Oct 2014, 12:29 AM
What I did was disable the updown feature of the control of the timepicker. Frankly, I should not have to do that, but it is what it is.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 29 Oct 2014, 03:38 PM
Hello Chris,

Thank you for writing.

RadTimePicker control allows the end user to enter only a time value to the editable area of the control. However, its Value property is typeof(DateTime) which contains date part as well. By default, this date part represents DateTime.Now and if you specify a different date it will be reset when changing the value. Note that this control is not purposed to manipulate dates. However, I have logged it in our feedback portal. You can track its progress, subscribe for status changes and add your vote/comment to it on the following link - feedback item.

I have also updated your Telerik points.

Currently, the possible solution that I can suggest is to use a RadDateTimePicker with time format. When changing the value you should keep manually the date part unchanged. Here is a sample code snippet demonstrating how to do that:
this.radDateTimePicker1.ShowUpDown = true;
this.radDateTimePicker1.Format = DateTimePickerFormat.Custom;
this.radDateTimePicker1.CustomFormat = "HH:mm tt";
this.radDateTimePicker1.Value = date;
 
this.radDateTimePicker1.ValueChanging += radDateTimePicker1_ValueChanging;

private DateTime date = new DateTime(2014, 5, 20, 8, 15, 30); 
 
private void radDateTimePicker1_ValueChanging(object sender, ValueChangingEventArgs e)
{
    DateTime newDate = (DateTime)e.NewValue;
    Console.WriteLine(newDate);
    if (newDate.Date != date.Date)
    {
        e.Cancel = true;
        this.radDateTimePicker1.Value = new DateTime(date.Year, date.Month, date.Day,
            newDate.Hour, newDate.Minute, newDate.Second);
    }
}
I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
ravi
Top achievements
Rank 1
answered on 05 Nov 2014, 03:07 PM
Hi ,

How to disable the dates for the future dates.So that when the future date is get selected it should not appear in the textbox. Please find the attached screenshot of hidden the dates.
Thanks in advance

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 10 Nov 2014, 11:04 AM
Hello Ravi,

Thank you for writing.

I suppose that your question is related to RadDateTimePicker and you are trying to hide all future dates in the popup calendar. This can be achieved by setting the CalendarCellElement.Opacity property to 0 for all cells that you do not want to be displayed. Additionally, in order to prevent changing the value with a future date, you can cancel the ValueChanging event. Here is a sample code snippet:
public Form1()
{
    InitializeComponent();
 
    this.radDateTimePicker1.Value = DateTime.Now;
    RadDateTimePickerCalendar calendarBehavior = this.radDateTimePicker1.DateTimePickerElement.GetCurrentBehavior() as RadDateTimePickerCalendar;
    calendar = calendarBehavior.Calendar;
    calendar.ViewChanged += Calendar_ViewChanged;
    this.radDateTimePicker1.Opening += radDateTimePicker1_Opening;
    this.radDateTimePicker1.ValueChanging += radDateTimePicker1_ValueChanging;
}
 
private void radDateTimePicker1_Opening(object sender, CancelEventArgs e)
{
    RefreshView();
}
 
RadCalendar calendar;
 
private void RefreshView()
{
    if (calendar != null)
    {
        MonthViewElement viewElement = calendar.CalendarElement.CalendarVisualElement as MonthViewElement;
 
        foreach (CalendarCellElement cellElement in viewElement.TableElement.Children)
        {
            if (cellElement.Date.Date > DateTime.Now.Date)
            {
                cellElement.Opacity = 0;
            }
            else
            {
                cellElement.Opacity = 1;
            }
        }
    }
}
 
private void Calendar_ViewChanged(object sender, EventArgs e)
{
    RefreshView();
}
 
 
private void radDateTimePicker1_ValueChanging(object sender, ValueChangingEventArgs e)
{
    DateTime newDate;
 
    if (DateTime.TryParse(e.NewValue + "", out newDate) && newDate.Date > DateTime.Now.Date)
    {
        e.Cancel = true;
    }
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Asked by
Chris
Top achievements
Rank 1
Answers by
Chris
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
ravi
Top achievements
Rank 1
Share this question
or