Correctly exhibiting date from datapicker - javascript

I've created a datapicker to allow user to choose date in a date field. The problem is that I'm cannot change the format date is shown after the user pick date in the calendar. What is shown is MM/DD/YYYY and what I want is YYYY-MM-DD.
I tried already this and this solution, without success.
forms.py
project_expiration = forms.DateField(required=False, widget=forms.TextInput(attrs={'class':'datepicker'}))
template
<script type="text/javascript">
$(function() {
$( ".datepicker" ).datepicker({
changeMonth: true,
changeYear: true,
format: 'yy-mm-dd',
minDate: new Date(),
});
});
</script>

I believe the format's property name of a datepicker widget is dateFormat, not format. It does look like you are using the correct format, however.

Related

Set minimum view mode for bootstrap datepicker

I am trying to open a datepicker that only allows months to be chosen, so I am trying to use the minViewMode option but it is not working. It allows me to view the days of each month.
$("#datepicker").datepicker({
format: "mm-yyyy",
minViewMode: 1,
changeMonth: true,
changeYear: true
});
This produces the below datepicker:
I am aiming for something similar to the following:

jquery datepicker set initial date

I need to set the initial date on jquery-ui datepicker initialization, so i tried with defaultDate field:
$('#first').datepicker({
defaultDate: '12/04/2018',
dateFormat: 'dd/mm/yy',
});
but no luck: DEMO
according to the API for defaultDate i can use:
a string in the format defined by the dateFormat option, or a relative
date
so, what i'm missing? is it a jquery bug or what?
PS: i know i can setDate after initialization:
$('#second').datepicker({
dateFormat: 'dd/mm/yy',
}).datepicker("setDate", '12/04/2018');
but that's not what i need. Any idea?
You can add a value to input field, and it will act like defaultDate and setDate at the same time
$('#some_input_element').val('12/04/2018').datepicker({
dateFormat: 'dd/mm/yy',
});
I have updatet your jsfiddle as fourth input element

how to add more than one calender in a single page using javascript

I am trying to add two calenders in a single page using javascript.
But it is not working for me for the second field. I tried the same for jQuery also. Still facing the same problem.
Can any one helpme . Thanks in advance.
Here is the code:
$(".datepicker").each(function () {
$(this).datepicker({
dateFormat: 'dd-mm-yy',
changeMonth: true,
changeYear: true,
});
});
You don't need to do this in loop (although that should also work.):
$(".datepicker").datepicker({
// your stuff here
});
Because class selectors returns a collection so this would work.
Sample Demo
What could be the issues:
What seems to me is most probably you have same ids to both the elements but still in this case your datepicker for both inputs will be intialized but on selection of a date only first field will be filled with dates if same id applied on every datepicker input.
Sample Demo with same ids.
Why not just this:
$(".datepicker").datepicker({
dateFormat: 'dd-mm-yy',
changeMonth: true,
changeYear: true
});
It should apply plugin for each control with 'datepicker' class

Prevent date selection in the past

How do we prevent the datepicker from picking a date in the past?
I used the following:
$("#Date").datepicker({ minDate: '0' });
$("#Date").datepicker({ minDate: new Date() });
but I can still select a date in the past.
aparently this is the way
$("#datepicker").datepicker({ minDate: 0 });
remove the quotes from the 0
Have you tried using the mindate and maxdate parameters?
JSFiddle here.
Take a look at the documentation and have a play with it.

jquery datepicker year range default

Having a bit of an issue with the JQuery datePicker, I suspect it's just the matter of a setting I've overlooked or got wrong.
If you look at this simple fiddle: JS Fiddle
You'll see I've set the year range, so that the by default when you click on the input it will open it up on 1994, but if you then click on any of those dates, e.g. 3rd Sept, it'll actually put it in the input as 2012 still, rather than the year that is selected in the drop down menu.
How can I make it so that it uses the correct year without having to change the drop down and then change it back again?
Cheers.
As others have suggested, you have to set the defaultDate. However, unlike the other solutions, you don't want to hardcode the date since your dropdown list will be updating as the years pass since you're doing a relative list.
Try this instead...
function loadDatePicker() {
$('.datePicker').datepicker({
dateFormat: 'yy-mm-dd',
changeYear: true,
changeMonth: true,
yearRange: "-18:-12",
defaultDate:"-18y-m-d" // Relative year/month/day
});
}
Weird, but adding defaultDate:"1994-01-01" to the datepicker options seems to fix it.
Fiddle
I think I got the reason..
It is because yearRange: just restrict the range in dropdown menu only.
It just change the list nothing else.
They mansion this over here..
http://jqueryui.com/demos/datepicker/#option-yearRange
[Note that this option only affects what appears in the drop-down, to restrict which dates may be selected use the minDate and/or maxDate options.]
To Prove this .. set date picker as below and you will find all date disable.
because it sawing us the calander of 2012 not 1994
$('.datePicker').datepicker({
dateFormat: 'yy-mm-dd',
changeYear: true,
changeMonth: true,
yearRange: "-18:-12",
maxDate: new Date(2012,1-1,1)
});
To fix it you have to use defaultDate as well as minDate and maxDate
i hope i am clear

Categories