Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team

I have a small problem, my input-form control for date is not correct. It shows this form on my input-daterange.eg 01/08/2020. I want this format be 08/01/2020 and have the below logic that does that and need some improvement to make it better.

What I have tried:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.0/css/bootstrap-datepicker3.min.css">
<script type='text/javascript' src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.0/js/bootstrap-datepicker.min.js"></script>
 <!---DatePicker for startDate and endDate.
   ----> 
   <div class="d-flex justify-content-start">
   <div class = "col-xl-10.5 col-lg-10.5 col-md-10 col-sm-10.5 col-10.5">
 <div class="input-daterange input-group" id="datepicker">
   <input type="text" class="input-sm form-control" name="from" placeholder="startdate"/>
   <span class="input-group-addon">To</span>
   <input type="text" class= "input-sm form-control" placeholder="enddate"/>
     </div>
     </div>
  </div><br>
  // date functionality
 $(document).ready(function() {
   var year = (new Date).getFullYear();
   $('.input-daterange').datepicker({
     dateFormat: "dd-mm-yy",
     autoclose:true,
     minDate: new Date(year, 0, 1),
     maxDate:new Date(year, 11, 31)

   });
 });
JavaScript

Posted
Updated 8-Jan-20 22:01pm

1 solution

***Update use the format option not dateFormat
hello, you already have the dateFormat option.
The format you already have on your script is
dd=day,
mm=month,
yy =year,

example date with your current format is : 31/12/2020

$(document).ready(function() {
   var year = (new Date).getFullYear();
   $('.input-daterange').datepicker({
     format: "dd-mm-yyyy",
     autoclose:true,
     minDate: new Date(year, 0, 1),
     maxDate:new Date(year, 11, 31)

   });
 });


you can change date format for example :
"mm-dd-yyyy"
or
"mm/dd/yyyy"
 
Share this answer
 
v3
Comments
gcogco10 9-Jan-20 4:12am    
On the form to me it reads as 01/07/2020. Instead it should be on a normal form when using datepicker 07/01/2020. Question is does bootstrap support this logic, the problem its not on datepicker on Jquery but on the form itself.
tninis 9-Jan-20 4:15am    
did you changed this :
$(document).ready(function() {
var year = (new Date).getFullYear();
$('.input-daterange').datepicker({
dateFormat: "dd-mm-yy",
autoclose:true,
minDate: new Date(year, 0, 1),
maxDate:new Date(year, 11, 31)

});
});

to this

$(document).ready(function() {
var year = (new Date).getFullYear();
$('.input-daterange').datepicker({
format: "dd-mm-yyyy",
autoclose:true,
minDate: new Date(year, 0, 1),
maxDate:new Date(year, 11, 31)

});
});

??

bootstrap datepicker has an option "format" not the option "dateFormat"
gcogco10 9-Jan-20 4:22am    
thanks mate, the format did work. it now reads as 15-01-2020 which is readable. I did know there is an option. Also when using the format:dd-mm-yyyy works to this approach unlike if using this format:mm/dd/yyyy

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900