Jquery datepicker in master page - javascript

I know there is so many similar question already been asked but none of them solved my problem. I tried so many times but the datepicker still not work.
So in my master page
<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"/>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<head/>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
In my content Page(head section inside asp content place holder 1)
// i tried this 4 way but still not work
// $("[id$=txtStart]")
// $("input[id$='date2']").datepicker();
<%--<%=txtStart.ClientID%>--%>
<%-- $("#<%=txtStart.ClientID %>")--%>
$(document).ready(function () {
$('[id$=txtStart]').datepicker({
minDate: 0,
dateFormat: 'dd/mm/yy',
numberOfMonths: 2,
onSelect: function (dateStr) {
var min = $(this).datepicker('getDate'); // Get selected date
min.setDate(min.getDate() + 1);
$('[id$=txtEnd]').datepicker('option', 'minDate', min); // Set other min, default to today
}
});
//another datepicker same as above
$('[id$=txtEnd]').datepicker({
.....
}
});
});
this is my control
<td class="auto-style3"><asp:TextBox ID="txtStart" runat="server" > </asp:TextBox>
<td class="auto-style3"><asp:TextBox ID="txtEnd" runat="server" > </asp:TextBox>

Your code should be
$(document).ready(function () {
$("#<%=txtStart.ClientID%>").datepicker({
minDate: 0,
dateFormat: 'dd/mm/yy',
numberOfMonths: 2,
onSelect: function (dateStr) {
var min = $(this).datepicker('getDate'); // Get selected date
min.setDate(min.getDate() + 1);
$("#<%=txtEnd.ClientID%>").datepicker('option', 'minDate', min); // Set other min, default to today
}
});
});

I've tested and works fine with this code:
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script>
$(document).ready(function() {
$("[id$=txtStart]").datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 2,
onClose: function (selectedDate) {
$("#to").datepicker("option", "minDate", selectedDate);
}
});
$("[id$=txtEnd]").datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 2,
onClose: function (selectedDate) {
$("#from").datepicker("option", "maxDate", selectedDate);
}
});
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div>
from <asp:TextBox ID="txtStart" runat="server"/> to <asp:TextBox ID="txtEnd" runat="server"/>
</div>
</asp:Content>
Thought I would suggest another datepicker range.
then your code would be:
<script>
$(document).ready(function () {
var dtStart = $('[id$=txtStart]'),
dtEnd = $('[id$=txtEnd]');
$('[id$=txtStart]').dateRangePicker(
{
separator: ' to ',
getValue: function() {
if (dtStart.val() && dtEnd.val())
return dtStart.val() + ' to ' + dtEnd.val();
else
return '';
},
setValue: function(s, s1, s2) {
dtStart.val(s1);
dtEnd.val(s2);
}
});
});
</script>
and your master page:
<link rel="stylesheet" href="//longbill.github.io/jquery-date-range-picker/daterangepicker.css"/>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"></script>
<script src="//longbill.github.io/jquery-date-range-picker/jquery.daterangepicker.js"></script>
result:

Related

How to show next 7 days while using datetimepicker?

function getNext7WorkingDays(){
var d = new Date();
var day = d.getDay();
if(day>=0 && day<=3) return 9;
else if(day!=6) return 10;
else return 11;
}
$('.datepicker_quiz_dataa').datetimepicker({
timepicker:false,
format:'d-m-Y',
formatDate:'Y/m/d',
minDate: 1,
maxDate: '+'+getNext7WorkingDays()+'D'
});
In this code I using datetimepicker where I am getting only date in d-m-y format and I want that it show current date to next 7 days dates only but now I am getting previous 14 days dates. So, How can I do this? Please help me.
Thank You
Try this below :
$('.datepicker_quiz_dataa').datepicker({
minDate: 0,
maxDate: "7d",
dateFormat: "d-m-y"
});
OR
$('.datepicker_quiz_dataa').datepicker({
minDate: 0,
maxDate: "1w",
dateFormat: "d-m-y"
});
Hope it helps
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>datepicker demo</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.12.4.js"></script>
<script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<div id="datepicker"></div>
<script>
$( "#datepicker").datepicker({
minDate: 0,
maxDate: 6,
dateFormat: "d-m-y",
onSelect : function(r) { console.log(r) }
});
</script>
</body>
</html>

Set default date for bootstrap datepicker

I am trying to open the bootstrap datepicker on the date stored in the var startDate.
The code below is not working as the default date format for the datepicker is mm/dd/yy. startDate is in the format dd/mm/yy.
<script type="text/javascript" >
function toggle_dp() {
$("#myModal").modal("show");
$("#datepicker").datepicker({
dateFormat: 'dd/mm/yy',
changeMonth: true,
changeYear: true,
defaultDate: new Date('{{ startDate }}')
});
$("#datepicker").datepicker( "option", "dateFormat", "dd-mm-yy" );
}
</script>
For example, if the startDate is 05/03/2018 (5th March), the datepicker will open with default date 03/05/2018 (3rd May).
Use formate: instead of dateFormat:
<script type="text/javascript" >
function toggle_dp() {
$("#myModal").modal("show");
$("#datepicker").datepicker({
format: 'dd/mm/yy',
changeMonth: true,
changeYear: true,
defaultDate: new Date(toMMDDYYYY(startDate))
});
}
function toMMDDYYYY(date) {
var datePart = date.split("/");
var MMDDYYYY = [datePart[1], datePart[0],datePart[2]].join('/');
return MMDDYYYY
}
</script>
You have written the wrong syntax for format. Just replace dateFormat with format in your script code like below
More help on Bootstrap Date-picker Format
<script type="text/javascript">
function toggle_dp() {
$("#myModal").modal("show");
$("#datepicker").datepicker({
format: 'dd/mm/yy',
changeMonth: true,
changeYear: true,
defaultDate: new Date('{{ startDate }}')
});
$("#datepicker").datepicker("option", "format", "dd-mm-yy");
}
</script>

Birthday Date Auto generate year for 18 years old

My question is how to automatically set the date range for 18 years old, like for example today is 2015, so the date range must be 1935-1997, don't mind the 1935. Because the date range is for 18 years old and above, so when it come to 2016 the date range will automatically set to 1935-1998 then so on so forth as the year comes by.
Here's the current code with javascript on it
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Select a Date Range</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( "#from2" ).datepicker({
defaultDate: "+1w",
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
yearRange: '1935:1997',
onClose: function( selectedDate ) {
$( "#to2" ).datepicker( "option", "minDate", selectedDate );
}
});
$( "#to2" ).datepicker({
defaultDate: "+1w",
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
yearRange: '1935:1997' ,
onClose: function( selectedDate ) {
$( "#from2" ).datepicker( "option", "maxDate", selectedDate );
}
});
});
</script>
</head>
<body>
<label for="birthday">Birthday</label>
<input type="text" id="from2" name="from">
</body>
</html>
Well as you can see you in my javascript function the range function is hardcoded. :(
Get the range:
var startDate = "1935";
var endDate = new Date().getFullYear() - 18;
var interval = startDate + ":" + endDate;
Set the range to the DatePicker:
yearRange: interval
Fiddle

Jquery datepicker and Spring MVC internationalization

Good day folks. Please help me. I have Jquery datepicker:
$.datepicker.setDefaults($.datepicker.regional["ru"]);
$("#dateinput").datepicker({
dateFormat: "yy-mm-dd",
beforeShowDay: beforeShowDayHandler,
showOn: 'both',
onClose: function (dateText, inst) {
$(this).attr("disabled", false);
},
beforeShow: function (input, inst) {
$(this).attr("disabled", true);
}
});
function beforeShowDayHandler(date) {
if (self.SelectedDayValue != -1) {
if (date.getDate() != 1) {
return [false, '', 'selected'];
}
}
return [true, ''];
}
jQuery(function ($) {
Presently I have locale on russian. How can I take locale parameter for my date picker on jsp age.
My jsp page:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><spring:message code="label.input.head" var="headTitle"/></title>
<script type="text/javascript" src="resources/jsFiles/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="resources/jsFiles/select2.js"></script>
<link rel="stylesheet" href="resources/cssFiles/select2.css"/>
<script type="text/javascript" src="resources/jsFiles/select2_e.js"></script>
<link rel="stylesheet" href="resources/cssFiles/inputStyle.css"/>
<script type="text/javascript" src="resources/jsFiles/jquery-ui.js"></script>
<script type="text/javascript" src="resources/jsFiles/jquery-ui-i18n.js"></script>
<link rel="stylesheet" href="resources/cssFiles/jquery-ui.css"/>
<script type="text/javascript">
$(document).ready(function() {
$.datepicker.setDefaults($.datepicker.regional['<%response.getLocale().getLanguage(); %>']);
$("#dateinput").datepicker({
dateFormat: "yy-mm-dd",
beforeShowDay: beforeShowDayHandler,
showOn: 'both',
onClose: function (dateText, inst) {
$(this).attr("disabled", false);
},
beforeShow: function (input, inst) {
$(this).attr("disabled", true);
}
});
function beforeShowDayHandler(date) {
if (self.SelectedDayValue != -1) {
if (date.getDate() != 1) {
return [false, '', 'selected'];
}
}
return [true, ''];
}
jQuery(function ($) {
console.log($('.widthclass').select2_e().on('change', function () { }));
});
});
</script>
</head>
<body>
//other omitted....
</body>
Spring servlet config locale:
<!-- Change my local over url variable lang. Example: ?lang=en -->
<interceptors>
<beans:bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:property name="paramName" value="lang"/>
</beans:bean>
</interceptors>
I hope this going to be enough information. I want when user change locale it will change datepicker locale automatically too. Thank you
Try to use request interceptor to populate model with locale:
public class PagePopulationInterceptor extends HandlerInterceptorAdapter {
#Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
if(modelAndView != null) {
Locale locale = response.getLocale();
modelAndView.addObject("currentLocale", locale.getLanguage());
}
}
}
Also you need some configuration for interceptor:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="your.package.util.PagePopulationInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
After that you can access locale language via ${currentLocale}
OR
Easier, but dirtier way is to use scriptlet:
<%= response.getLocale().getLanguage() %>
You dont need to register intercepter, but it is not clean to have java code in your jsp.

jquery ui date picker limit to Sundays

I have looked at some of the answers here to this type of question but could not get them to work how I needed them to. I need to have my jQuery UI datepicker only allow Sundays in the past to be selected. Is this possible to do?
Thank you
// Enable Sunday only
$("#datepickerID").datepicker({
dateFormat: 'dd-mm-yy',
minDate: 1,
beforeShowDay: enableSUNDAYS
});
// Custom function to enable SUNDAY only in jquery calender
function enableSUNDAYS(date) {
var day = date.getDay();
return [(day == 0), ''];
}
It's not exactly your situation, but contains what you need to know to do what you need to do:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/base/jquery-ui.css"
type="text/css" media="all" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"
type="text/javascript"></script>
<script type="text/javascript">
$(function () {
// 0 = monday, 1 = tuesday, 2 = wednesday, 3 = thursday,
// 4 = friday, 5 = saturday, 6 = sunday
var daysToDisable = [2, 4, 5];
$('#<%= txtDate.ClientID %>').datepicker({
beforeShowDay: disableSpecificWeekDays
});
function disableSpecificWeekDays(date) {
var day = date.getDay();
for (i = 0; i < daysToDisable.length; i++) {
if ($.inArray(day, daysToDisable) != -1) {
return [false];
}
}
return [true];
}
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
</form>
</body>
</html>

Categories