How to display only date in KendoDateTime picker - asp.net-mvc

I've placed a kendo DateTime Picker on a web page.
According to the requirement initially it should display only date and if user needs then he/she can select the time as well.
Below is the code which initially fill the kendo datetime picker:
<input id="txtDate" type="text" maxlength="10" style="width: 250px;" value="" />
<script type="text/javascript">
$('#txtDate').kendoDateTimePicker({ value: new Date() });
</script>
The above code fill date and time. But i want to fill only current date.
I tried with the following code to check if it can fill only date. But it adds the 12:00 AM with date.
$('#txtDate').kendoDateTimePicker({ value: new Date('04/30/2017') });
Is there any way to fill only date in Kendo DateTime picker?

What you are looking is kendoDatePicker, which will allows user to select only date.
$(document).ready(function()
$("#txtDate").kendoDatePicker();
});
Edit: or add formatting property to kendoDateTimePicker, which will effect how value is displayed for user.
$('#txtDate').kendoDateTimePicker({
value: new Date(),
format: 'MM/dd/yyyy'
});

If you never need to display the time one possible option might be to store the datetime value as an attribute of the current widget and on the submit event of the form manually ovverride the values:
<script>
$(document).ready(function () {
$("#datetimepicker").kendoDateTimePicker({
format: "yyyy/MM/dd",
change: function(data){
data.sender.element.attr("value", data.sender.value());
}
});
});
$("form").on("submit", function(){
$("input").each(function(){
var inputValue = $(this).attr("value");
if(inputValue){
$(this).val(inputValue);
}
});
})
</script>
If you want to display the time after the user pick a time you can change the format of the widget subscribing a function to the open event:
<input id="datetimepicker" />
<script>
$("#datetimepicker").kendoDateTimePicker({
format: "yyyy/MM/dd",
value: new Date(),
open: function(e){
if(e.view == "time"){
this.setOptions({format: "yyyy/MM/dd HH:mm"})
}
if(e.view == "date"){
this.setOptions({format: "yyyy/MM/dd"})
}
}
});

You can build on the answers given above and detect when a time is selected then reapply a display format in javascript.

Via jQuery:
$('#txtDate').datetimepicker({
format: 'L'
});

Instead of using Kendo.DateTimePickerFor you can use DatePickerFor.
This way you can get rid of hour part.
#(Html.Kendo().DatePickerFor(model => model.StartDate).HtmlAttributes(new { #class = "form-control" , required="required"}))

Related

Datetimepicker - How to set stored database date instead of today date

I am using 'bootstrap3-datetimepicker-rails' gem and I would like set my stored date in the input datetimepicker instead of today date.
This is my code:
$(document).on('dp.change','input.gas_ref_date', function(e){
$('input#datetimepicker_gas_ref_date').datetimepicker({maxDate: moment(), format:'DD-MM-YYYY' });
});
});
But always the datetimepicker shows a today date.
I have tried this other solution, but does not works:
$(document).on('dp.change','input.gas_ref_date', function(e){
var my_stored_date = this.value;
$('input#datetimepicker_gas_ref_date').datetimepicker({maxDate: moment(), format:'DD-MM-YYYY', defaultDate: new Date(my_stored_date) });
});
});

The date format of bootstrap-dateppicker is not effective on page load

My datepicker textbox is generated in ASP.NET MVC:
#Html.TextBoxFor(model => model.StartDate, new {#class = "form-control datepicker", placeholder = "Pick a date...", data_date_format="mm/dd/yyyy"})
'.datepicker()` is then called:
$().ready(function() {
$('.datepicker').datepicker();
});
It appears there is the format in place, just not in the beginning.
When the page is first loaded, the textbox shows 4/24/2015 12:00:00 AM. But when a date is picked, the format then applies and shows a date without the time stamp.
Is there any option or setting I've missed?
If you wanted the simplicity of handling this all on the client with the native format you've chosen for the datepicker, you can just trigger the formatting by setting the date to itself on page load:
// update and date to trigger formatting
$('#myDate').datepicker('update',$('#myDate').datepicker("getDate"))
Alternatively, here's an extension method you can add that will reformat the date in an control when called:
$.fn.reformatDate = function(settings) {
return this.each(function() {
$this = $(this);
if (!$this.data('datepicker')) return
$this.datepicker('setDate',$this.datepicker("getDate"))
});
};
Then use it like this:
$('#myDate').reformatDate();
Demo in Stack Snippets
$.fn.reformatDate = function(settings) {
return this.each(function() {
$this = $(this);
if (!$this.data('datepicker')) return
$this.datepicker('setDate',$this.datepicker("getDate"))
});
};
$(function() {
// simulate pre-filled date from server
$("#myDate").val(new Date());
// initialize like normal
$('#myDate').datepicker();
// update date to trigger formatting
$('#myDate').reformatDate();
});
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet"/>
<link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.1/css/datepicker.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.1/js/bootstrap-datepicker.js"></script>
<input id="myDate" type="text" class="datepicker" />

Is there anyway to grab the default value of the slider handle if it is not moved?

I am using JQuery UI sliders in a mobile survey application. I can correctly capture the slider value if the label is clicked or the handle is moved to the label. My problem is that if the user does not click on a label/move the slider. I want to record the initial value of the slider. Here is the html where the slider goes:
<div class="well-lg">
<b>#Html.DisplayFor(model => model.QuestionsText)</b><div id="mySlideOutput"></div>
<br />
<div id="mySlideOutput"></div> // testing output of slider value
<br />
#*Include ID of EvaluationQuestion in ID of slider*#
<div id="slider-#(Model.ID)"></div>
#Html.Hidden("QR-" + #Model.ID)
</div>
Here is the Jquery code:
<script>
var items = ['Very Exceptional Results', 'Above Standard', 'Standard', 'Results Not Acceptable', 'Less Than Standard', 'N/A'];
#*var sliderDefault = $("#slider-#(Model.ID)"),
initialValue = 1;*#
// commented out code that was used to make sure the value is being populated at certain states
//NOTE: Needs to be changed to populate slider item labels from database.
$("#slider-#(Model.ID)").slider({
value: initialValue,
min: 1,
max: 6,
//step: 1,
slide: function (event, ui) {
$("#QR-#(Model.ID)").val(ui.value);
$('#mySlideOutput').html('ui-value = ' + ui.value);
},
#*stop: function(event, ui) {
$("#QR-#(Model.ID)").val(ui.value);
//alert(ui.value);
},*#
#*start: function(event, ui) {
$("#QR-#(Model.ID)").val(ui.value);
alert(ui.value);
}*#
});
$("#slider-#(Model.ID)").slider("pips", { rest: "label", labels: items });
I would like the initial value to populate the db if the slider is not moved to another option. Any suggestions? I appreciate the help.
$("#slider-#(Model.ID)").slider("value");
This will just return the value of the slider at any time it is called... whether you want to call this function at run time, or just before page unload is up to you.

How do I stop the datepicker from displaying by default if no date is entered

I am using jquery-ui-1.10.4.js and ASP MVC.
In my View I am accepting an optional date as input;
#Html.EditorFor(model => model.Assessment.SsipAccreditationDate)
I have an Editor Template which injects the datepicker class;
#model DateTime?
#Html.TextBox("", (Model.HasValue ? Model.Value.ToLongDateString() : string.Empty),
new { #class = "datePicker", #style = "width:200px;" })
I have jquery code to hook up the datepicker with the date field;
if ($(".datePicker").length) {
$(".datePicker").datepicker({ showOn: 'both', dateFormat: constantDateFormat, changeMonth: true, changeYear: true })
.next('button.ui-datepicker-trigger')
.attr("tabIndex", "-1");
}
When the View is loaded, if this date field is empty the datepicker automatically displays. What I would prefer would be for the datepicker to only be displayed if the user clicks on it or the associated button. This is what the showOn: Both configuration is meant to acheive.
Notice in the jquery the tab index is set to -1, so the reason for the datepicker showing cannot be to do with the field in focus.
So how do I stop the datepicker showing when the View is loaded?
You can disable it by using defaultDate attribute.
$(".datePicker").datepicker({
defaultDate: ''
})
I do not think this is the best answer, but I found I could do this by disabling the datepicker initially, and then enabling it once it has been clicked on;
$(document).ready(function () {
initialiseEdit();
});
var initialiseEdit = function () {
// Disable the datepicker to ensure the datepicker is not shown when the page is loaded
$('#Assessment_SsipAccreditationDate').datepicker("disable");
// Class to stop the control from looking disabled
$('#Assessment_SsipAccreditationDate').addClass('inputBox');
};
// This code is to make the datepicker function as though it is enabled, whether it is or not.
$('.clickEnable').click(function () {
$('#Assessment_SsipAccreditationDate').datepicker("enable");
$('#Assessment_SsipAccreditationDate').datepicker("show");
});
Because '#Assessment_SsipAccreditationDate' is disabled, I had to wrap it with a span containing the clickEnable class and put the click event on that.

How to combine jquery date-picker and jquery full calendar

Can any one tell me how I could update a selection in a date picker to a full calendar ie When user selects date from DatePicker then FullCalendar loads events for given date.
Take a look at this : Issue 167: Mini Calender to control Main Calender.
Or make the combination with following example :
$(document).ready(function() {
$("#fullcalendar").fullCalendar({
// .....
});
$('#datepicker').datepicker({
inline: true,
onSelect: function(dateText, inst) {
var d = new Date(dateText);
$('#fullcalendar').fullCalendar('gotoDate', d);
}
});
}
<div id="fullcalendar"></div>
<div id="datepicker"></div>
The "gotoDate (method)" would help you select a date programatically in fullcalendar. So let the user select a date using jquery date picker and register a onchange event on the text input that captures the date. When the onchange fires, use gotoDate to set the date in fullcalendar. Refer - http://arshaw.com/fullcalendar/docs/current_date
This might help anyone coming to this answer to add a gotoDate datepicker to fullcalendar as a custom button
customButtons: {
gotoDate: {
text: 'Go to date',
click: function () {
$(this).datepicker({
autoclose: true
});
$(this).datepicker().on('changeDate', function (e) {
$('#fullcalendar').fullCalendar('gotoDate', e.date);
});
$(this).datepicker('show');
}
},
},
footer: {
'center': 'gotoDate'
},

Resources