Calendar not starting from today jqueryui daterangepicker - javascript

I am using the jQuery UI Daterangepicker (reference).
I want the calendar to start from today, with no max date. I am trying to use moment in the calendar for the dates.
This is the HTML & JS code I have but it sets the end date on today with no minDate.
<input id="search-vac-daterange" name="search-vac-daterange">
$("#search-vac-daterange").daterangepicker({
minDate: moment(),
startDate: moment()
});
I have also tried with minDate: new Date() and startDate: new Date(). No results either. The dates stay with today as end date.
NOTE: moment is working because a console.log(moment()); returns me a moment object:
p {_isAMomentObject: true, _isUTC: false, _pf: {…}, _locale: j, _d: Fri Jan 18 2019 16:14:35 GMT+0100 (Midden-Europese standaardtijd), …}
EDIT: I have tried the suggestions as given below, none of them have an impact on the solution.
dateFormat: 'dd/mm/yy',
minDate: moment().format('DD/MM/YYYY'),
startDate: moment().format('DD/MM/YYYY'),
or
minDate: new Date(moment("11-02-1993").format("YYYY-MM-DD")),
startDate: new Date(moment("11-02-1994").format("YYYY-MM-DD")),
Added image to show the problem:

You have two issues. Firstly moment() returns a Moment object which is not a valid value for the minDate or startDate properties of the picker. To fix this just pass a standard Date object.
Secondly you need to provide options to the underlying jQueryUI datepicker control within the datepickerOptions object:
$("#search-vac-daterange").daterangepicker({
datepickerOptions: {
minDate: new Date(),
startDate: new Date(),
maxDate: '+1y' // required for future dates to be selectable
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript" src="//code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
<script type="text/javascript" src="http://tamble.github.io/jquery-ui-daterangepicker/daterangepicker-master/jquery.comiseo.daterangepicker.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/2.3.1/moment.min.js"></script>
<input id="search-vac-daterange" name="search-vac-daterange">

Related

make the default single date picker date

I have a case where I use the daterangepicker library to retrieve the date, here is the jsfiddle link: https://jsfiddle.net/heriira_/0bgqxdLa/5/
$(function () {
var start = moment().subtract(1, "days");
$("#demo").daterangepicker(
{
singleDatePicker: true,
startDate: start,
},
function (start, end, label) {
console.log(start.format("YYYY-MM-DD"));
}
);
});
in the code above how to make a handle if, for example, the default value is yesterday, and when we choose a time that is more than yesterday, for example two days before it will return to the default, which is yesterday's date.
based on the documentation https://www.daterangepicker.com/#options
you just need to add minDate:
so the date before yesterday can't be selected
$(function() {
var start = moment().subtract(1, "days");
$("#demo").daterangepicker({
singleDatePicker: true,
startDate: start,
minDate: start
},
function(start, end, label) {
console.log(start.format("YYYY-MM-DD"));
}
);
});
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />
<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<input type="text" id="demo" value="10/24/1984" />

Modifying JavaScript Calendar in Qualtrics

I found a JavaScript code to embed a calendar on a Qualtrics question.
Enter a survey date:
<link href="https://cdn.jsdelivr.net/npm/pikaday/css/pikaday.css" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pikaday/1.6.1/pikaday.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/locale/es.js"></script>
<script>
Qualtrics.SurveyEngine.addOnload(function()
{
var inputId = 'QR~' + this.questionId;
moment.locale('en');
var picker = new Pikaday(
{
field: document.getElementById(inputId),
i18n: {
previousMonth : 'previous month',
nextMonth : 'next month',
months : moment.localeData()._months,
weekdays : moment.localeData()._weekdays,
weekdaysShort : moment.localeData()._weekdaysShort
},
minDate: new Date(2020, 05, 22),
maxDate: new Date(2020, 05, 26),
yearRange: [2000, 2020],
bound: true,
container: document.getElementById('container')
});
});
Qualtrics.SurveyEngine.addOnReady(function()
{
jQuery("#"+this.questionId+" .InputText").attr("readonly",true);
});
</script>
It worked well on the first page. However, the calendar still appeared on the left corner on the second page of my Qualtrics survey. How should I modify the above code so that the calendar only appears on the date question on the first page only?
Also, I found that the "5" in "new Date(2020, 05, 22)" is for June (not May). I think it might be due to the logic that the first number is 0 in Javascript. How should I modify the code to display the month number matching the actual month?
Where did you put the script? The link and script tags should go in the survey header. Then the addOnload and addOnReady functions in the question's JavaScript (not inside script tags in the question text).
Yes, in that date format month is indexed from 0. You could use a date string the with actual dates like "2020-05-22".
Personally, I like to use flatpickr as a date picker with Qualtrics.

Month and year picker value changes when field is clicked into for second time

I have some code for a month and year date picker which i got from this site, i have edited the date format (mm/dd/yy) and selecting the values works great, however when i click away from the field and back to it the picker changes the date considerably.
For example, the following invokes the picker with todays date and you click 'Done' which puts todays date into the input field, if you click away and then click the input field again the date picker comes up with December last year, do it again and its december the previous year, etc...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css"/>
<script type="text/javascript">
$(function() {
$('.date-picker').datepicker( {
changeMonth: true,
changeYear: true,
showButtonPanel: true,
dateFormat: 'dd/mm/yy',
onClose: function(dateText, inst) {
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1));
},
beforeShow : function(input, inst) {
var datestr;
if ((datestr = $(this).val()).length > 0) {
year = datestr.substring(datestr.length-4, datestr.length);
month = jQuery.inArray(datestr.substring(0, datestr.length-5), $(this).datepicker('option', 'monthNamesShort'));
$(this).datepicker('option', 'defaultDate', new Date(year, month, 1));
$(this).datepicker('setDate', new Date(year, month, 1));
}
}
});
});
</script>
<style>
.ui-datepicker-calendar {
display: none;
}
</style>
</head>
<body>
<label for="startDate">Date :</label>
<input autocomplete="off" name="startDate" id="startDate" class="date-picker" />
</body>
</html>
I'm new to JavaScript so i am struggling to find out exactly where the issue is.
Changed the following line;
month = jQuery.inArray(datestr.substring(0, datestr.length-5), $(this).datepicker('option', 'monthNamesShort'));
To this to pick up the right month and it works;
month = $(this).datepicker('getDate').getMonth();

how to convert jquery datepicker format to timestamp format like dd-mm-yyyy (28/09/2019 3:40pm)

I dont wanna manual convertion, i have the jquery datepicker v.1.12.1
if i select the date from datepicker it will automatically convert timestamp format.
i want a date format in timestamp like 28/09/2019 3:40 GMT-0400 this format to 1569668513 this format.
Can you please help me?
var dateString = '#referdate',
dateTimeParts = dateString.split(' '),
timeParts = dateTimeParts[1].split(':'),
dateParts = dateTimeParts[0].split('/'),
date;
date = new Date(dateParts[2], parseInt(dateParts[1], 10) - 1, dateParts[0], timeParts[0], timeParts[1]);
console.log(date.getTime()); //1379426880000
console.log(date); //28-09-2019 3:40 GMT-0400
You can try this.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<script>
function getTimeAndDate(){
let today = new Date();
let date = today.getDate()+'/' + (today.getMonth()+1) + '/'+today.getFullYear();
let h = today.getHours();
let m =today.getMinutes();
console.log(h+':'+m);
console.log(date);
}
</script>
<body>
<input type="button" onclick="getTimeAndDate()" value="Button">
</body>
</html>
Are you looking for the dateFormat option?
From the docs:
$( ".selector" ).datepicker({
dateFormat: "yy-mm-dd"
});
$( "#datepicker" ).datepicker({
dateFormat: "#"
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script
src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"
integrity="sha256-0YPKAwZP7Mp3ALMRVB2i8GXeEndvCq3eSl/WsAl1Ryk="
crossorigin="anonymous"></script>
<input id="datepicker">
If you just want to log the timestamp of the chosen date, you can get the javascript date object directly from the picker on change.
// however you initialize the datepicker does not matter
$("#datepicker").datepicker();
// when a date is picked the change event is fired
$("#datepicker").change(function() {
// get the date object
let theDate = $("#datepicker").datepicker("getDate");
console.log(theDate.getTime())
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js" integrity="sha256-0YPKAwZP7Mp3ALMRVB2i8GXeEndvCq3eSl/WsAl1Ryk=" crossorigin="anonymous"></script>
<input id="datepicker">

unable to set calendar start date in pikaday calendar

When i trigger the start date input box, I want to show the future or past month in calendar popup as a starting date in end date calendar.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://dbushell.com/Pikaday/css/pikaday.css">
<link rel="stylesheet" href="https://dbushell.com/Pikaday/css/site.css">
</head>
<body>
<input type="text" id="datepicker">
<br/>
<input type="text" id="datepicker2">
<script src="https://cdn.jsdelivr.net/momentjs/2.14.1/moment-with-locales.min.js"> </script>
<script src="https://dbushell.com/Pikaday/pikaday.js"> </script>
<script>
var picker = new Pikaday({
field: document.getElementById('datepicker'),
format: 'D MMM YYYY',
minDate: new Date(),
onSelect: function (date) {
picker2.setMinDate(date);
//picker2.???? to set the starting month of picker2 calendar
}
});
var picker2 = new Pikaday({
field: document.getElementById('datepicker2'),
format: 'D MMM YYYY',
minDate: new Date(),
onSelect: function () {
console.log(this.getMoment().format('Do MMMM YYYY'));
}
});
</script>
</body>
</html>
please check the above example, based on start day selection i want to trigger the end date calendar starting month. please suggest.
JS bin link
As I understand your question, you want to set the selected month in picker to the picker2 object. use the API onOpen.
Example
var picker = new Pikaday({
field: document.getElementById('datepicker'),
format: 'D MMM YYYY',
minDate: new Date(),
onSelect: function(date) {
//console.log(this.getMoment().format('Do MMMM YYYY'));
console.log('test ' + date);
picker2.setMinDate(date);
//picker2.setStartRange(date)
}
});
var picker2 = new Pikaday({
field: document.getElementById('datepicker2'),
format: 'D MMM YYYY',
minDate: new Date(),
onOpen: function() {
picker2.gotoDate(picker.getDate())
},
onSelect: function() {
console.log(this.getMoment().format('Do MMMM YYYY'));
}
});
When I worked with Pikaday package in Vue.Js project I had to set default date to my date field in form. I tried many different versions of setting default date. But they didn't work for me. Finally I found the solution for my situation. The solution was to set prop like auto-default="autoDefault" here autoDefault is equels to true to Pikaday component like this...
<vue-pikaday v-model='date' auto-default="autoDefault" :options="pikadayOptions" />
data() {
return {
autoDefault : true,
pikadayOptions: {
format : 'YYYY/MM/DD',
minDate : moment().toDate(),
},
}
},

Categories