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

Send FocusedDate to January 1

3 Answers 23 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 31 May 2016, 03:04 PM

How can I set the FocusedDate to January 1 of the current year?  I have tried a JavaScript function, but the calendar will have the current month as the first month.

The JavaScript is

function navigateToJanuary(sender) {
    var todaysDate = new Date();
    var triplet;
    triplet = [todaysDate.getFullYear(), 01, 01];
    sender.navigateToDate(triplet);
 }

I put an alert box to verify that I am getting the correct date. 2016,1,1

I have  in the RadCalendar

<ClientEvents OnLoad="navigateToJanuary" />

Am I using the wrong client event?

3 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 01 Jun 2016, 01:19 PM
Hello Jeff,

If you would like to have the date as selected you would also need to call the selectDate() method. Check out the following snippets as illustration:

Markup:

<telerik:RadCalendar runat="server" ID="RadCalendar1" >
    <ClientEvents OnLoad="clientLoad" />
</telerik:RadCalendar>

JavaScript:

function clientLoad(sender, args) {
    var todaysDate = new Date();
    var calendar = sender;
 
    calendar.navigateToDate([todaysDate.getFullYear(), 1, 1]);
    calendar.selectDate([todaysDate.getFullYear(), 1, 1]);
     
}



Regards,
Viktor Tachev
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Jeff
Top achievements
Rank 1
answered on 03 Jun 2016, 04:02 PM

That works.  However, when I select another year, it returns back to January 2016.  Is there another function I need to put the code?

Jeff

0
Viktor Tachev
Telerik team
answered on 06 Jun 2016, 01:17 PM
Hello Jeff,

Indeed the date will be reverted back to January 1st after post with the current implementation. If you would like to keep the selected date in the calendar you would need to add a condition that checks if there is an already selected date. This way January 1st will not be selected after the user selects a date.

function clientLoad(sender, args) {
    if (sender.get_selectedDates().length == 0) {
        var todaysDate = new Date();
        var calendar = sender;
 
        calendar.navigateToDate([todaysDate.getFullYear(), 1, 1]);
        calendar.selectDate([todaysDate.getFullYear(), 1, 1]);
    }
}


Regards,
Viktor Tachev
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
Calendar
Asked by
Jeff
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Jeff
Top achievements
Rank 1
Share this question
or