[Solved] Is it possible for the datepicker to default open to Time page?

2 Answers 357 Views
DateTimePicker
Grant
Top achievements
Rank 1
Grant asked on 08 Feb 2022, 04:05 PM
I have a cases where I need the users to be able to change the date and time, but mostly will just be editing the time?  Is there a way to default open to the Time Page rather than opening the date page every time?

2 Answers, 1 is accepted

Sort by
0
Hristian Stefanov
Telerik team
answered on 11 Feb 2022, 09:52 AM

Hi Grant,

I understand the need in such a scenario. You have right.

Currently, the DateTimePicker doesn't have the possibility to open the Time page by default. As a result, I opened a feature request on your behalf on our Public Feedback Portal:

Allow the DateTimePicker to open the Time portion/page by default

This feature will allow you to achieve the desired result.

We prioritize feature requests based on community demands and interests. I already added your vote there.

You are also automatically subscribed to receive email notifications for status updates.

Regards,
Hristian Stefanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Andrew
Top achievements
Rank 2
Iron
answered on 11 Oct 2024, 10:22 PM

FYI, I found a javascript work-around that seems to allow the TIME tab to be auto-selected when the picker is opened. First set the OnOpen event:

<TelerikDateTimePicker Id="expectedCheckInTime" @bind-Value="CheckoutCrewViewModel.ExpectedCheckinTime" Format="MMM dd, hh:mm tt" OnOpen="@OnCheckInDateTimePickerOpen">

<DateTimePickerSteps Minute="5" />
</TelerikDateTimePicker>

In your @code:

@inject IJSRuntime JsRuntime

private async Task OnCheckInDateTimePickerOpen(DateTimePickerOpenEventArgs args)
{
await JsRuntime.InvokeVoidAsync("switchToTimeTab", "expectedCheckInTime");
}

In your script.js file:

function switchToTimeTab(pickerId) {
    setTimeout(function () {
        const picker = document.querySelector('.k-datetime-wrap-md');
        if (picker) {
            const timeTab = picker.querySelector('.k-button-group-stretched button.telerik-blazor.k-button:not(.k-selected) span.k-button-text');
            if (timeTab && timeTab.textContent.trim() === 'Time') {
                timeTab.closest('button').click();
            }
        }
    }, 100); // Slight delay before clicking to allow popup to appear.
}

Credit goes to Claude AI for that solution (GPT wasn't able to figure it out). Hope that helps! :)

 

Hristian Stefanov
Telerik team
commented on 14 Oct 2024, 07:57 AM

Hi Andrew,

Thank you for sharing the workaround you have found. This will help other developers facing the same scenario.

Your effort is highly appreciated.

Kind Regards,

Hristian

Andrew
Top achievements
Rank 2
Iron
commented on 27 Oct 2026, 07:58 AM

p.s. at some point in time it appears a telerik update broke this. This is the new javascript that works with the Telerik UI For Blazor version 15.0.1 :)

function switchToTimeTab() {
    setTimeout(() => {
        const timeButton = [...document.querySelectorAll(
            '.k-datetimepicker-popup .k-segmented-control-button:not(.k-selected)')]
            .find(b => b.textContent.trim() === 'Time');

        timeButton?.click();
    }, 100); // Slight delay so the popup is rendered first.
}

Though it looks a little wonky because of the animation Telerik has added to make the time part "scroll" into view so it you don't like that whirlwind effect as the calendar whooshes away while the picker pops up, there is a more complicated fix:

function switchToTimeTab() {
    document.body.classList.add('dt-switching');

    const reveal = () => document.body.classList.remove('dt-switching');
    const started = performance.now();
    let target = null;

    const tick = () => {
        if (target) {
            // Switched — let it paint in place, then show it.
            if (target.querySelector('.k-time-tab')) {
                requestAnimationFrame(reveal);
                return;
            }
        } else {
            // The popup that just opened is the one still on the Date tab.
            // A previous picker's popup can linger in the DOM while it closes.
            target = [...document.querySelectorAll('.k-datetimepicker-popup')]
                .find(p => p.querySelector('.k-date-tab')) ?? null;

            const timeButton = target && [...target.querySelectorAll('.k-segmented-control-button')]
                .find(b => b.textContent.trim() === 'Time');

            if (timeButton) timeButton.click();
            else target = null; // not rendered yet, try next frame
        }

        // Safety valve so the popup never stays hidden.
        if (performance.now() - started > 1000) {
            reveal();
            return;
        }

        requestAnimationFrame(tick);
    };

    requestAnimationFrame(tick);
}
with CSS:
body.dt-switching .k-datetimepicker-popup {
    visibility: hidden;
}

    body.dt-switching .k-datetimepicker-popup * {
        transition: none !important;
    }

of course, it'll all break the next time Progress changes their css name tags again :) enjoy!
Tags
DateTimePicker
Asked by
Grant
Top achievements
Rank 1
Answers by
Hristian Stefanov
Telerik team
Andrew
Top achievements
Rank 2
Iron
Share this question
or