Change date that appears on a datepicker

Asked

Viewed 1,239 times

0

I have the following datepicker:

var dataIni = document.querySelector("#calendarioIni");
var dataFim = document.querySelector("#calendarioFim");

function checaVazio(){
  var botao = document.querySelector("form input[type='submit']");
  botao.style.display = dataIni.value && dataFim.value ? "inline" : "none";
}

var start = new Date(1998, 00, 01);
var end = new Date(1998, 11, 31) ;

$(document).ready(function() 
{
    $('#calendarioIni').datepicker({
        format: 'dd/mm/yyyy',
        startDate: '01/01/1998', // 1 de Janeiro de 1998
        endDate: '31/12/1998', // 31 de Janeiro de 1998,
        defaultViewDate: { year: 1998, month: 0, day: 1 },
        onSelect: checaVazio
    });
});

$(document).ready(function() 
{
    $('#calendarioFim').datepicker({
        defaultViewDate: new Date(1998, 0, 1),
        endDate: end,
        startDate: start,
        onSelect: checaVazio
    });
});

I would like to change the month that appears when I open the calendar (currently when I click opens in December, I wanted to change to January), I tried to do this with "startView" but it didn’t work. So I use it:

<form method="post" action="">
      Início do período:
      <input type="text" id="calendarioIni" name="dataInicio">
      Fim do período:
      <input type="text" id="calendarioFim" name="dataFim"> 
      <input style="display: none;" type="submit" value="Consultar" />                                    
      <br><br><br>

      <?php if(isset($_POST['dataInicio']) && isset($_POST['dataFim']))
      {
            $dataIni = $_POST['dataInicio'];
            $dataFim = $_POST['dataFim'];
            echo $dataIni."<br>";//Teste para verificar o valor nas variáveis que recebem a data via POST
            echo $dataFim."<br>";
            } 
     ?>                                    
</form>
  • Add to your question by explaining which datepicker component you are using. If you can, create executable code in jsfiddle.net.

  • What did you mean by component? If it is bootstrap?

  • component even because datepicker is not something native to Javascript. For example, it may be the Datepicker of Jquery UI or it may be another.

  • So, using bootstrap and jquery, I basically call it <script type='text/javascript' src='.. /js/plugins/bootstrap/bootstrap-datepicker.js'></script>

  • You wouldn’t have to $(document).ready(function () { ... }); twice. You can do everything within a single block. And the defaultViewDate of #calendarioFim is incorrect. You have to use the same format as the #calendarioIni, that is, pass an object with the attributes year, month and day.

  • i tbm use for a Datatable, could put together too?

  • You can. If you share the same event, you can put.

  • I get it, but they don’t share the same event

  • I just can’t understand the following: I went to the datepicker class and made some modifications, for example, I changed the language, but I don’t apply this modification... the behavior is kind of random, there are changes that I make that change and others don’t

  • If all three are within the scope of one $(document).ready(function () { ... });, then share the same event that is when the GIFT is fully charged. There you can unite into one.

  • but do not share... the first generates a table to generate a pdf and the second is to display the calendar and the user select the dates

  • If you reference your datepicker this way (pointing to a file in the local JS folder) there is no way to know which component it is. There are dozens of datepicker components that use bootstrap so it is interesting you indicate which site you downloaded from because each has different configuration and behavior.

  • @Pagotti worse than there is no way I know where it was downloaded, because I "inherited" this project

Show 8 more comments

1 answer

1


Date class

The class Date of Javascript accepts several parameters that will be used in your constructor. One of the problems is that the parameter referring to months uses an interval of 0-11, being 0 January and 11 the month of December.

So when you start that way: var start = new Date(1997, 12, 01); will generate an unexpected date and using the console.log(start); will result in: 1998-01-01T02:00:00.000Z, that is to say, 01/01/1998 and not 01/12/1997.

You can instantiate the class object Date in various ways, for example:

let data = new Date('January 1, 2018'); // 1 de Janeiro de 2018

Or:

let data = new Date('2018-01-01'); // 1 de Janeiro de 2018

Read more about the class Date in the w3schools: Javascript Dates


Datepicker

Already in the Datepicker, the correct option to set the start date is the defaultDate.

Read more about the plugin Datepicker of jQuery: Datepicker Widget


EDIT:

Try to do it that way:

$(function () {
  $('#calendarioIni').datepicker({
    format: 'dd/mm/yyyy',
    startDate: '01/01/1998', // 1 de Janeiro de 1998
    endDate: '31/12/1998', // 31 de Dezembro de 1998,
    defaultViewDate: { year: 1998, month: 0, day: 1 } // Coloca o foco em 1 de Janeiro de 1998
  });
});
<link type="text/css" rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link type="text/css" rel="stylesheet" href="https://uxsolutions.github.io/bootstrap-datepicker/bootstrap-datepicker/css/bootstrap-datepicker3.min.css">

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript" src="https://uxsolutions.github.io/bootstrap-datepicker/bootstrap-datepicker/js/bootstrap-datepicker.min.js"></script>

<form>
  Início do período:
  <input type="text" name="dataInicio" id="calendarioIni">
</form>

  • I added here the dafaultDate and it didn’t work... the jquery I already call through the footer as follows: <script src=".. /vendor/jquery/jquery.js"></script> and dou um include("footer.php"); on the page

  • The code posted here works. Edit your question with the updated code, please.

  • the only thing I did was add the defaultDate and the code didn’t work... I believe the problem may be something in relation to jquery

  • Take off the startDate and the endDate and test again. I edited my answer with the date you set and works normally.

  • removed, and the date that appears is the current month

  • when I call jquery the way you did not even show the calendar

  • Which plugin of Datepicker are you using? What I used is the official of jQuery.

  • where could I see this? my jquery header has this: /Eslint-disable no-unused-vars/ /*! * jQuery Javascript Library v3.1.0 * https://jquery.com/ * * Includes Sizzle.js * https://sizzlejs.com/ * * Copyright jQuery Foundation and other Contributors * Released under the MIT License * https://jquery.org/license * * Date: 2016-07-07T21:44Z */

  • That’s the jQuery. You need to see if there’s any other script being included in your code, for example: <script type="text/javascript" src="path/to/datepicker.js"></script>

  • <script type='text/javascript' src='.. /js/plugins/bootstrap/bootstrap-datepicker.js'></script>

  • we could go to chat?

  • I edited the answer for this plugin you are using. Please take a look.

  • but the user will be able to select any day of the year, so it is limited to January

  • It’s just an example. You can adjust the ranges of the startDate and endDate as your need.

  • ah yes, but I would like it to appear in January and not in December

  • I edited again, take a look and see if this is how you need it.

  • there is something very strange in my code, it didn’t work that "defaultViewDate" nor got highlighted in my IDE

  • Put your code back on, please.

  • I will edit the post with it complete

Show 14 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.