0

I have a case there I want to select Month and Day from the UI to create recurring task.No need to select a year.28 days should be displayed in February.I am using Bootsrap for building the UI.I am initially thought of considering below option for implementing the same

  1. Create one Dropdown where All the Months will be displayed (ie Jan,Feb,Mar..Dec)

  2. Create another dropdown for loading Days.(1,2...31). This dropdown should be reloaded based on Month selection.

Is it possible to do the same using Tempus-Dominus Datetime picker ?? https://getdatepicker.com/5-4/

I have created sample fiddle .Where i want display similar like (Mar - 31) after selecting. Year should not be there in DateTimepicker.(By default consider Year as 2022.But it should not be displayed anywhere in Datetimepicker.)

      $('#datetimepicker1').datetimepicker({

       format: 'MM-DD'
    });

https://jsfiddle.net/8ck1tanb/1/

Can anyone suggest better user friendly design/approach for implementing this functionality. It will be more helpful if you can share sample code if it is already implemented.

vmb
  • 2,878
  • 15
  • 60
  • 90
  • this would be a dirty workaround by replacing the year in the string but maybe it helps? https://jsfiddle.net/4L6hvqyn/ – toffler Mar 15 '22 at 14:37
  • @toffler your solution is great..I will test it thoroughly..I have added two more fields while intitlaizing datetimepicker. var year = (new Date).getFullYear(); $('#datetimepicker1').datetimepicker({ format: 'MMMM-DD', keepOpen: true, minDate: new Date(year, 0, 1), maxDate: new Date(year, 11, 31) });.So that user can select only date within year 2022 – vmb Mar 16 '22 at 12:25
  • @toffler...can you post it as answer instead of comment – vmb Mar 16 '22 at 12:25
  • @toffler..While testing i found one issue..I want to disable selection of date 29th of Feb (in case if it is leap year).How can i achieve this?Disable a a specific date? – vmb Mar 16 '22 at 12:33

1 Answers1

1

You can make use of the tempus-dominus-datetimepicker events to split the month and the year and show only the month.

Here is an example fiddle.

   $('#datetimepicker1').on('show.datetimepicker update.datetimepicker change.datetimepicker',function(){
     if(typeof $(this).find('.picker-switch').html() !== "undefined"){
       $(this).find('.picker-switch').html($(this).find('.picker-switch').html().split(" ")[0]);
     };    
   });
  • show.datetimepicker is triggered when the popup opens.
  • update.datetimepicker is triggered when you use the 'next' 'previous' arrows
  • change.datetimepicker is triggered when you select a date

UPDATE

This fiddle has the 29th of Febuary disabled for the next 3 leap years You can simply disable specific dates with the disabledDates option.

$('#datetimepicker1').datetimepicker({
    disabledDates: [new Date('2024-02-29'),new Date('2028-02-29'),new Date('2032-02-29')]
});
toffler
  • 1,231
  • 10
  • 27
  • While testing i found one issue..I want to disable selection of date 29th of Feb (in case if current year is leap year).How can i achieve this ?Disable a a specific date considering current year? – vmb Mar 16 '22 at 12:36
  • Please find updated fiddle https://jsfiddle.net/xkev6ga9/1/ ..Here i want to handle the Leap year case.I want to disable selection of date 29th of Feb (in case if current year is leap year).How can i achieve this ?Disable a a specific date considering current year – vmb Mar 16 '22 at 12:38
  • you can disable it or maybe even just hide it. https://jsfiddle.net/05xngwsq/2/ you can find the col using this selector: $(this).find('td[data-day^="02/29"]').hide(); this is an example where it's hidden – toffler Mar 16 '22 at 13:01
  • or even easier, using css to hide this: https://jsfiddle.net/jhucy16z/ – toffler Mar 16 '22 at 13:09
  • I tested your hiding trick..that solution has some issue(Try to make 03/31 as hidden and check what happens to future dates.Check how April calender looks). The remaining date order will get change.So that solution won't work.I just want to make that date as non selectable – vmb Mar 16 '22 at 14:02
  • @toffler..I think as you suggested, we need to disable it ,instead of hiding . $(this).find('td[data-day^="03/31"]').prop( "disabled", true ); – vmb Mar 16 '22 at 14:10
  • 1
    Please have a look at my update – toffler Mar 16 '22 at 14:50
  • 1
    @toffler..Thank you so much my friend – vmb Mar 16 '22 at 15:22