Display jquery ui date value in separate elements - jquery-ui

I need to show the selected date value in different 'divs', 'spans', or 'p' elements like my example in the link below:
<button id="buttonHere">datepicker</button>
<div id="datepicker"></div>
<div id="resultdate">00/00/00</div>
<div id="resultdate_expectation">
<span>Aug</span>
<span>31</span>
<span>2018</span>
</div>
https://jsfiddle.net/alonsoct/qphza/464/
How can I show the date value separately?
Thanks

You can get the parts of the date from the datepicker's date attribute in the onSelect function:
$("#datepicker").hide();
$("#buttonHere").click(function() {
$("#datepicker").toggle();
});
$("#datepicker").datepicker({
onSelect: function(value, date) {
const months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
$('#resultdate_expectation span:eq(0)').html(months[date.selectedMonth]);
$('#resultdate_expectation span:eq(1)').html(date.selectedDay);
$('#resultdate_expectation span:eq(2)').html(date.selectedYear);
$("#datepicker").hide();
}
});
jsFiddle example

All you need is to set the dateFormat option properly:
dateFormat: "M d yy",
M - month name short
d - day of month (no leading zero)
yy - year (four digit)
See more: http://api.jqueryui.com/datepicker/#utility-formatDate
Based on your example, you did not specify if you wanted leading zero or not. I assumed not.
https://jsfiddle.net/Twisty/qphza/481/
$(function() {
// Set up datepicker toggle functionality
$("#datepicker").hide();
$("#buttonHere").click(function() {
$("#datepicker").toggle();
});
$("#datepicker").datepicker({
dateFormat: "M d yy",
onSelect: function(value, date) {
$('#resultdate').html(value);
$("#datepicker").hide();
}
});
});
Working as requested. Hope that helps.

Related

Add class to every nth element when i select date

How i can add class to every date under selected day.
Lets say i click on Thursday and all dates under Thursday have some class.
I have service where you can chose frequency, weekly, 2 weeks, 4 weeks, and when user click on some date add select frequency the dates are highlighted on the calendar.
I have try to add css to every nth child from ui-state-default but this is not working.
```
$('#cookdate').datepicker({
numberOfMonths: 2,
altField: '#selectedDatecook',
minDate: 2,
maxPicks: 20,
onSelect: function (date) {
var date = $(this).datepicker('getDate');
var day = date.getUTCDay();
},
});
You will want to use a combination of onSelect and beforeShowDate. For example:
$(function() {
var selected, final;
var days = 7;
$('#cookdate').datepicker({
numberOfMonths: 2,
minDate: 2,
onSelect: function(dStr, obj) {
selected = $.datepicker.parseDate("mm/dd/yy", dStr);
final = new Date(selected);
final.setDate(selected.getDate() + days);
$("#selectdate").val($.datepicker.formatDate("mm/dd/yy", final));
},
beforeShowDay: function(dt) {
$(".highlighted").removeClass("highlighted");
if (dt > selected && dt <= final) {
return [true, "highlighted"];
} else {
return [true, ""];
}
}
});
});
td.highlighted, td.highlighted a.ui-state-default {
background-color: #ccc;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<p>Cook Date: <input type="text" id="cookdate"> Select Date: <input type="text" id="selectdate"></p>
You can change days to be any number of days you want. beforeShowDays will then look for the matching dates and add a class to them, highlighted.

How to restrict jqueryui date range?

I have two date-pickers #from and #to, as usual, now what I need to do is make sure that the date range selected between the two is always within 3months of each other. So basically, on selecting one of the pickers, I need to set minDate/maxDate option for the other one relative to the selected date, But I don't know what would be the best way to find these relative dates. Any suggestions?
Alternatively you could do it like this:fiddle
Both ways are valid, i suppose.
.js
$("#startDatePicker").datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function(dateText,dateObj){
var dateObject = $(this).datepicker("getDate");
dateObject.setMonth(dateObj.currentMonth+3);
$("#endDatePicker").datepicker( "option", "maxDate",dateObject);
}
});
$("#endDatePicker").datepicker({
dateFormat: 'yy-mm-dd',
changeMonth: true,
onSelect: function(dateText,dateObj){
var dateObject = $(this).datepicker("getDate");
dateObject.setMonth(dateObj.currentMonth-3);
$("#startDatePicker").datepicker( "option", "minDate",dateObject);
}
});
I have created a jsfiddle.The range between the days is set to 3 days.you can edit based on ur requirement
$(document).ready(function () {
$("#startDatePicker").datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function (date) {
if ($('#endDatePicker').val() == "") {
var selectedDate = new Date(date);
var secsPerDay = 86400000;
var endDate = new Date(selectedDate.getTime() + 2 * secsPerDay);
alert(endDate);
$("#endDatePicker").datepicker("option", "minDate", selectedDate);
$("#endDatePicker").datepicker("option", "maxDate", endDate);
}
}
});
$("#endDatePicker").datepicker({
dateFormat: 'yy-mm-dd',
changeMonth: true,
onSelect: function (date) {
if ($('#startDatePicker').val() == "") {
var selectedDate = new Date(date);
var secsPerDay = 86400000;
var startDate = new Date(selectedDate.getTime() - 5 * secsPerDay);
$("#startDatePicker").datepicker("option", "minDate", startDate);
$("#startDatePicker").datepicker("option", "maxDate", selectedDate);
}
}
});
});
and HTML
<p>Start Date:
<input type="text" id="startDatePicker"/>
</p>
<p>End Date:
<input type="text" id="endDatePicker"/>
</p>

jQuery-ui datepicker range issue

I have 2 input boxes; one for the start date and the other for the end date. What I need is that when user selects the first date, the second date should automatically switch to the month of the first date. I am using the following script; and whenever I choose a date from the past, the second box pops up with today's date.
Any ideas?
Thanks
$(function() {
var dates = $( "#from, #to" ).datepicker({
defaultDate: "+1w",
changeMonth: true,
changeYear: true,
onSelect: function( selectedDate ) {
var option = this.id == "from" ? "minDate" : "maxDate",
instance = $( this ).data( "datepicker" ),
date = $.datepicker.parseDate(
instance.settings.dateFormat ||
$.datepicker._defaults.dateFormat,
selectedDate, instance.settings );
dates.not( this ).datepicker( "option", option, date );
}
});
});
One way of solving the problem might be to change the second value together with the first value whenever the first value is changed by user. This can be accomplished by adding few lines at the end of onSelect function.
if ( this.id == "from" ) {
dates.not( this ).datepicker( "setDate", date );
}
Working example http://jsfiddle.net/5gbpA/

jQuery UI DatePicker output date format

I have inline jQuery UI DatePicker and need to customize format and look of output date, code:
<div id="date-start"></div>
<div class="date start" id="date-start-output"></div>
$(function(){
// Datepicker
$('#date-start').datepicker({
inline:true,
showOtherMonths: true,
altField: "#date-start-value",
altFormat: "dd M yy",
dateFormat: "dd M yy",
onSelect: function(){
var day1 = $("#date-start").datepicker('getDate').getDate();
var month1 = $("#date-start").datepicker('getDate').getMonth() + 1;
var year1 = $("#date-start").datepicker('getDate').getFullYear();
var str_output = "<b>" + day1 + "</b><span>" + month1 + "<br />" + year1 + "</span>";
$('#date-start-output').html(str_output);
}
});
});
It works, but in #date-start-output I have date format not as "dd M yy" but as "dd m yy". How can set necessary output format for month?
Also I need to show in #date-start-output current date after page load, I'm trying "beforeShow" event, but it not work together with "onSelect" event for me.
Thanks.
onSelect event handler first parameter is the selected date as text.
I guess it would be formatted as specified during the initialization
See http://jqueryui.com/demos/datepicker/#event-onSelect
$(function(){
// Datepicker
$('#date-start').datepicker({
inline:true,
showOtherMonths: true,
altField: "#date-start-value",
altFormat: "dd M yy",
dateFormat: "dd M yy",
onSelect: function(dateText){
$('#date-start-output').html(dateText);
}
});
});
If you need special formatting, you can try:
$(function(){
// Datepicker
$('#date-start').datepicker({
...
onSelect: function(){
var dateText = $.datepicker.formatDate("<b>dd</b><span>M<br />yy</span>", $(this).datepicker("getDate"));
$('#date-start-output').html(dateText);
}
});
});
Not sure it will work with HTML tags though

set minDate of next datepicker instance based on previous datepicker date + 1 day

HTML
<label for="checkIn">Check in</label>
<input type="text" readonly="readonly" name="checkIn" id="checkIn" />
<label for="checkOut">Check out</label>
<input type="text" readonly="readonly" name="checkOut" id="checkOut" />
javaScript
var dates = $( "#checkIn, #checkOut" ).datepicker({
autoSize: true,
minDate: "+0",
maxDate: "+6M",
onSelect: function( selectedDate ) {
var option = this.id == "checkIn" ? "minDate" : "maxDate",
instance = $( this ).data( "datepicker" ),
date = $.datepicker.parseDate(
instance.settings.dateFormat
|| $.datepicker._defaults.dateFormat,
selectedDate, instance.settings );
dates.not( this ).datepicker( "option", option, date );
}
});
now what I want that, whenever a user set any date for checkIn, checkOut should be next to that selected date along with all other options.
see the fiddle for the same
I tried a lot but not succeed. Please help me what logic to apply?
Try to separate the fields and initiate datepicker separately, and then, try this:
$('#date1').datepicker({
constrainInput: true,
dateFormat: 'D, dd M yy',
// Once change, set date2 min date
onSelect: function (dateText, inst) {
$('#date2').datepicker("option", "minDate", dateText);
}
});
$('#date2').datepicker({
constrainInput: true,
minDate: 0,
dateFormat: 'D, dd M yy'
});
Hope this help :)
shennyL answer didn't account for selected date + n.
So:
On your #checkIn place:
minDate: 0, //sets the minDate offset to 0, meaning today.
onSelect: function() {
var date = $(this).datepicker('getDate');
if (date) {
date.setDate(date.getDate() + 1);
$("#checkOut").datepicker("option", "minDate", date)
}
}
This works quite ok as far as I can tell. You may just need to clean up some code on your fiddle perhaps ?

Resources