Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

140
Vistas
Set current date in datepicker on page load

I will have a date picker on a form page using a datepicker (calendar bootstrap)

Now when you click on the click button, it opens and you can select a date.

But I need to have the current date instead of the click inscription when loading the page. How can I do that?

I need it to be exactly selected, as if we clicked on today's date. Because inside the calendar there will be another function that works when a date is selected

I tried installing "setDate", new Date() and other answers from this thread bootstrap datepicker today as default, but nothing worked for me..

let restaurantReserve = {
    init: function() {
        let _self = this;

        $('#reservation-date').datepicker({
            startDate: '+0d'
        }).on('changeDate', function(e) {
            const arDate = e.date.toString().split(' ');
            let input = $('[name="RestaurantReservationForm[date]"]');
            input.val(arDate[3] + '-' + (e.date.getMonth() + 1) + '-' + arDate[2]);
            _self.unSetError(input);
            $('#reservation-date .js-value').text(arDate[2] + ' ' + arDate[1]);
        });
    },
    setError: function(ob) {
        $('#' + ob.data('btnId')).addClass('btn-error');
    },
    unSetError: function(ob) {
        $('#' + ob.data('btnId')).removeClass('btn-error');
    }
}

restaurantReserve.init();
.btn {
    border: none;
    border-radius: 8px;
    padding: 10px 15px;
    font-weight: 800;
    font-size: 14px;
    cursor: pointer;
}

.btn-fourth {
    text-decoration: none;
    background: #e3e5e8;
    color: #747b8b;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script>

<a class="btn btn-fourth " id="reservation-date" data-date=">">
  <span class="icon br-calender"></span> <span class="js-value">click</span>
</a>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

  • You need to get the current date in the same format as that of the datepicker.
  • You can then set the current date of the datepicker to today's date (from above) once the datepicker has been initialized.

Note: the value in .datepicker('setValue', value) must be of the same format as that of the datepicker format option. Eg. 26 Apr in your case

let restaurantReserve = {
    init: function() {
        let _self = this;
        let today = new Date().toLocaleString('en-us',{day:'numeric', month:'short'}).split(' ').reverse().join(' '); // eg. Apr 26

        $('#reservation-date').datepicker({
            startDate: '+0d'
        }).on('changeDate', function(e) {
            const arDate = e.date.toString().split(' ');
            let input = $('[name="RestaurantReservationForm[date]"]');
            input.val(arDate[3] + '-' + (e.date.getMonth() + 1) + '-' + arDate[2]);
            _self.unSetError(input);
            $('#reservation-date .js-value').text(arDate[2] + ' ' + arDate[1]);
        });

        $("#reservation-date").datepicker("setDate", today);
    },
    setError: function(ob) {
        $('#' + ob.data('btnId')).addClass('btn-error');
    },
    unSetError: function(ob) {
        $('#' + ob.data('btnId')).removeClass('btn-error');
    }
}

restaurantReserve.init();
.btn {
    border: none;
    border-radius: 8px;
    padding: 10px 15px;
    font-weight: 800;
    font-size: 14px;
    cursor: pointer;
}

.btn-fourth {
    text-decoration: none;
    background: #e3e5e8;
    color: #747b8b;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script>

<a class="btn btn-fourth " id="reservation-date" data-date=">">
  <span class="icon br-calender"></span> <span class="js-value">click</span>
</a>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda