How to initialize a time in daterangepicker [duplicate] - javascript

Hello I was asked to modify some code. We got something like this:
$("#expiration_datepicker").datetimepicker( "option", "disabled", false ).attr('value', '');
$("#expiration_datepicker").datetimepicker({
dateFormat: 'mm-dd-yy',
showOn: 'button',
buttonImage: '../chassis/images/calendar.gif',
buttonImageOnly: true,
minDate: 0,
maxDate: '+5Y',
duration: '',
<c:if test="${formIsReadonly or form.newsItemId == '-1'}">
disabled: true,
</c:if>
constrainInput: false,
timeFormat: 'hh:mm'
});
Looks like this is setting up the date picker
How Can I set it up to default show todays date and time??
like dd-m-yy hh:mm

just use setdate and new Date() for date/time right now
$(function() {
$("#datepicker1").datetimepicker({
dateFormat: 'dd-m-yy'
// your options
}).datetimepicker("setDate", new Date());
});​
FIDDLE

Check the datepickers documentation for more customizations. Surely it have..
$("#expiration_datepicker").datetimepicker("option", "disabled", false).attr('value', '');
$("#expiration_datepicker").datetimepicker({
**dateFormat: 'dd-m-yy',** // date format goes here
showOn: 'button',
buttonImage: '../chassis/images/calendar.gif',
buttonImageOnly: true,
minDate: 0,
maxDate: '+5Y',
duration: '',
<c:if test="${formIsReadonly or form.newsItemId == '-1'}">
disabled: true,
</c:if>
constrainInput: false,
timeFormat: 'hh:mm' // time format here
});
Usage:
<script>
$('#expiration_datepicker').click(function() {
('#expiration_datepicker').datetimepicker('setDate', (new Date()));
});
</script>

Need to add .datepicker("setDate", "0") after the datetimepicker call. see the code below.
$("#expiration_datepicker").datetimepicker( "option", "disabled", false ).attr('value', '');
$("#expiration_datepicker").datetimepicker({
dateFormat: 'dd-m-yy hh:mm', // format goes here. Check its documentation for more.
showOn: 'button',
buttonImage: '../chassis/images/calendar.gif',
buttonImageOnly: true,
minDate: 0,
maxDate: '+5Y',
duration: '',
<c:if test="${formIsReadonly or form.newsItemId == '-1'}">
disabled: true,
</c:if>
constrainInput: false,
timeFormat: 'hh:mm'
}).datetimepicker("setDate", "0");

var d = new Date();
$("#VALUATION_DATETIME").datetimepicker({
showSecond: false,
dateFormat: "dd/mm/yy",
timeFormat: 'HH:mm',
}).datetimepicker("setDate", new Date());
Output:- 14/04/2022 17:40

Set date programmatically:
$('#inputID').datetimepicker("setDate", "05.01.2017 13:36");

Related

jQuery UI Datepicker weird behavior shows weird icons

i have a weird behavior with date picker , and not reprduce for all computers,
the problem after select date from start date and then try to select from the end date , for some computers there additional arrow icons that appear in the date picker.
picture attached.
now the problem didn't reprduce for all , also it reprduce when get the minDate or maxDate related if you in start or end date
i've tested the issue on two computers that have a same os and browser versions but it reproduced on one of them
var from = $(".from", container).datepicker({
defaultDate: defaultDate,
dateFormat: "dd-M-yy",
changeMonth: true,
numberOfMonths: 3,
showOn: "both",
buttonImage: "/resources/images/forms/calendar-regular.png",
buttonText: "Start Date",
minDate : minDate,
maxDate: -1,
onClose: function (selectedDate) {
$(".to", container).datepicker("option", "minDate", selectedDate);
if (!$(".to", container).val()) {
$(".to", container).datepicker('setDate', selectedDate);
}
}
});
var to = $(".to", container).datepicker({
defaultDate: defaultDate,
dateFormat: "dd-M-yy",
changeMonth: true,
numberOfMonths: 3,
showOn: "both",
buttonImage: "/resources/images/forms/calendar-regular.png",
buttonText: "End Date",
minDate: minDate,
maxDate: 0,
onClose: function (selectedDate) {
$(".from", container).datepicker("option", "maxDate", selectedDate);
}
});
},
i'm using jquery-ui-1.9.1.js

I want this date picker to choose dates from years between 1990 to 2000

Can someone help me correct this code. I want to pick dates that are between the years 1990 and 2000 only without allowing other dates like the ones in future to be selected.
<script>
$(document).ready(function(){
$( "#EmpBirthdate" ).datepicker({
dateFormat: 'yy-mm-dd',
showOn: "button",
buttonImage: "images/calendar.gif",
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
yearRange: "-30"
});
});
</script>
$(function () {
$('#datepicker').datepicker({
dateFormat: 'yy-mm-dd',
showButtonPanel: true,
changeMonth: true,
changeYear: true,
yearRange: '1999:2012',
showOn: "button",
buttonImage: "images/calendar.gif",
buttonImageOnly: true,
minDate: new Date(1999, 10 - 1, 25),
maxDate: '+30Y',
inline: true
});
});
Just added year range option. It should solve the problem
You can use the min max attributes
Min Max
$( "#datepicker" ).datepicker({ minDate: value1, maxDate: value2 });

Jquery datepicker with angular returns undefined

In one Form I'm developing using angular, i use two datepickers
<td><input ng-model="reportsVal.start_date" type="text" id="startDatePicker" ng-disabled="showVals"></td>
<td><input ng-model="reportsVal.end_date" type="text" id="endDatePicker" required ng-disabled="showVals"></td>
$( "#startDatePicker").datepicker({
showOn: "button",
buttonImage : "images/icons/calender.png",
buttonImageOnly: true,
buttonText: "Start Date",
dateFormat: 'dd/mm/yy',
minDate: dateToday,
numberOfMonths: 1
});
$( "#endDatePicker").datepicker({
showOn: "button",
buttonImage : "images/icons/calender.png",
buttonImageOnly: true,
buttonText: "End Date",
dateFormat: 'dd/mm/yy',
minDate: dateToday
});
on submit when i console log reportsVal.start_date and reportsVal.end_date it comes as expected. But when i use end_date must be greater than start_date condition like below
$( "#startDatePicker").datepicker({
showOn: "button",
buttonImage : "images/icons/calender.png",
buttonImageOnly: true,
buttonText: "Start Date",
dateFormat: 'dd/mm/yy',
minDate: dateToday,
numberOfMonths: 1,
onSelect: function(selected) {
var date = $(this).datepicker('getDate');
if (date) {
date.setDate(date.getDate() + 1);
}
$("#endDatePicker").datepicker("option","minDate", date)
}
});
$( "#endDatePicker").datepicker({
showOn: "button",
buttonImage : "images/icons/calender.png",
buttonImageOnly: true,
buttonText: "End Date",
dateFormat: 'dd/mm/yy',
minDate: dateToday,
onSelect: function(selected) {
var date = $(this).datepicker('getDate');
if (date) {
date.setDate(date.getDate() - 1);
}
$("#startDatePicker").datepicker("option","maxDate", date || 0)
}
});
i'm getting undefined for these values reportsVal.start_date and reportsVal.end_date

jquery datepicker set date to tomorrow's date

I have set the date for #arrival to todays date, but how can I set the #departure to tomorrow's date?
$(function() {
$( "#arrival" ).datepicker({
dateFormat: "dd/mm/yy",
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
yearRange: ":2016",
minDate: "dateToday",
onClose: function( selectedDate ) {
$( "#departure" ).datepicker( "option", "minDate", selectedDate);
}
});
$(function() {
$("#arrival").datepicker("setDate", "0");
});
$( "#departure" ).datepicker({
dateFormat: "dd/mm/yy",
changeMonth: true,
changeYear: true,
numberOfMonths: 2,
yearRange: ":2016",
});
$(function() {
$("#departure").datepicker("setDate", "1");
});
});
I have customized the datepicker and it works fine.
In the change function of the first datepicker, create a date object, set the date one day forward, and set the date of the second datepicker to that date. You can use minDate to make sure any date earlier than the set date can not be picked.
$(function () {
$("#arrival").datepicker({
dateFormat: "dd/mm/yy",
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
yearRange: ":2016",
minDate: "dateToday",
onClose: function (selectedDate) {
var myDate = $(this).datepicker('getDate');
myDate.setDate(myDate.getDate()+1);
$('#departure').datepicker('setDate', myDate);
}
});
$("#departure").datepicker({
dateFormat: "dd/mm/yy",
changeMonth: true,
changeYear: true,
numberOfMonths: 2,
yearRange: ":2016",
});
$("#arrival").datepicker("setDate", "0");
$("#departure").datepicker("setDate", "1");
});
FIDDLE

How to change date format (MM/DD/YY) to (YYYY-MM-DD) in date picker

I have following datepicker script:
<script>
$(function(){
$("#to").datepicker();
$("#from").datepicker().bind("change",function(){
var minValue = $(this).val();
minValue = $.datepicker.parseDate("mm/dd/yy", minValue);
minValue.setDate(minValue.getDate()+1);
$("#to").datepicker( "option", "minDate", minValue );
})
});
</script>
Now dateformat is MM/DD/YY .how to change the date format to YYYY-MM-DD?
Use the dateFormat option
$(function(){
$("#to").datepicker({ dateFormat: 'yy-mm-dd' });
$("#from").datepicker({ dateFormat: 'yy-mm-dd' }).bind("change",function(){
var minValue = $(this).val();
minValue = $.datepicker.parseDate("yy-mm-dd", minValue);
minValue.setDate(minValue.getDate()+1);
$("#to").datepicker( "option", "minDate", minValue );
})
});
Demo at http://jsfiddle.net/gaby/WArtA/
Try the following:
$('#to').datepicker({
dateFormat: 'yy-mm-dd'
});
You'd think it would be yyyy-mm-dd but oh well :P
I had the same issue and I have tried many answers but nothing worked.
I tried the following and it worked successfully :
<input type=text data-date-format='yy-mm-dd' >
$( ".selector" ).datepicker( "option", "dateFormat", 'yy-mm-dd' );
See:
http://jqueryui.com/demos/datepicker/
and
http://docs.jquery.com/UI/Datepicker/formatDate#utility-formatDate
If in jquery the dateformat option is not working then we can handle this situation in html page in input field of your date:
<input type="text" data-date-format='yyyy-mm-dd' id="selectdateadmin" class="form-control" required>
And in javascript below this page add your date picker code:
$('#selectdateadmin').focusin( function()
{
$("#selectdateadmin").datepicker();
});
You need something like this during the initialization of your datepicker:
$("#your_elements_id").datepicker({ dateFormat: 'yyyy-mm-dd' });
this also worked for me. Go to the bootstrap-datepicker.js.
replace this code :
var defaults = $.fn.datepicker.defaults = {
autoclose: false,
beforeShowDay: $.noop,
calendarWeeks: false,
clearBtn: false,
daysOfWeekDisabled: [],
endDate: Infinity,
forceParse: true,
format: 'mm/dd/yyyy',
keyboardNavigation: true,
language: 'en',
minViewMode: 0,
multidate: false,
multidateSeparator: ',',
orientation: "auto",
rtl: false,
startDate: -Infinity,
startView: 0,
todayBtn: false,
todayHighlight: false,
weekStart: 0
};
with :
var defaults = $.fn.datepicker.defaults = {
autoclose: false,
beforeShowDay: $.noop,
calendarWeeks: false,
clearBtn: false,
daysOfWeekDisabled: [],
endDate: Infinity,
forceParse: true,
format: 'yyyy-mm-dd',
keyboardNavigation: true,
language: 'en',
minViewMode: 0,
multidate: false,
multidateSeparator: ',',
orientation: "auto",
rtl: false,
startDate: -Infinity,
startView: 0,
todayBtn: false,
todayHighlight: false,
weekStart: 0
};
I used this approach to perform the same operation in my app.
var varDate = $("#dateStart").val();
var DateinISO = $.datepicker.parseDate('mm/dd/yy', varDate);
var DateNewFormat = $.datepicker.formatDate( "yy-mm-dd", new Date( DateinISO ) );
$("#dateStartNewFormat").val(DateNewFormat);
Try this:
$.datepicker.parseDate("yy-mm-dd", minValue);
Use .formatDate( format, date, settings )
http://docs.jquery.com/UI/Datepicker/formatDate
just add dateFormat:'yy-mm-dd' to your .datepicker({}) settings, your .datepicker({}) can look something like this
$( "#datepicker" ).datepicker({
showButtonPanel: true,
changeMonth: true,
dateFormat: 'yy-mm-dd'
});
});
</script>
Just need to define yy-mm-dd here. dateFormat
Default is mm-dd-yy
Change mm-dd-yy to yy-mm-dd. Look example below
$(function() {
$( "#datepicker" ).datepicker({
dateFormat: 'yy-mm-dd',
changeMonth: true,
changeYear: true
});
} );
Date: <input type="text" id="datepicker">
Thanks for all. I got my expected output
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css" type="text/css" media="all">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js"></script>
<script>
$(function(){
$("#to").datepicker({ dateFormat: 'yy-mm-dd' });
$("#from").datepicker({ dateFormat: 'yy-mm-dd' }).bind("change",function(){
var minValue = $(this).val();
minValue = $.datepicker.parseDate("yy-mm-dd", minValue);
minValue.setDate(minValue.getDate()+1);
$("#to").datepicker( "option", "minDate", minValue );
})
});
</script>
<div class="">
<p>From Date: <input type="text" id="from"></p>
<p>To Date: <input type="text" id="to"></p>
</div>
this worked for me.
$('#thedate').datepicker({
dateFormat: 'dd-mm-yy',
altField: '#thealtdate',
altFormat: 'yy-mm-dd'
});
This work for me:
$('#data_compra').daterangepicker({
singleDatePicker: true,
locale: {
format: 'DD/MM/YYYY'
},
calender_style: "picker_4", },
function(start, end, label) {
console.log(start.toISOString(), end.toISOString(), label);
});
This is what worked for me in every datePicker version, firstly converting date into internal datePicker date format, and then converting it back to desired one
var date = "2017-11-07";
date = $.datepicker.formatDate("dd.mm.yy", $.datepicker.parseDate('yy-mm-dd', date));
// 07.11.2017
For me in datetimepicker jquery plugin format:'d/m/Y' option is worked
$("#dobDate").datetimepicker({
lang:'en',
timepicker:false,
autoclose: true,
format:'d/m/Y',
onChangeDateTime:function( ct ){
$(".xdsoft_datetimepicker").hide();
}
});
$('#selectdateadmin').focusin( function()
{
$("#selectdateadmin").datepicker();
});

Categories