Datepicker change date value and formatting rails 4 - javascript

I've made form to change date or city, but datepicker doesn't work properly. Here is link: http://leszczyna.wzks.uj.edu.pl/12_stolarski/events_for_seniors/pl/events?utf8=%E2%9C%93&search=Krak%C3%B3w&start_date=11-06-2015
Problem is with datepicker, when you click on it, it changes date to another format and year is set to 1911.
Here is my js:
$('document').ready(function() {
if ($('body.events').length) {
/* Datepicker for changing date in events */
$('.date').datepicker({
'language': "pl",
'todayHighlight': true,
'format': 'dd-mm-yyyy',
'autoclose': true
})
}
And part of my erb file
<div class="form-group">
<label class="sr-only" for="date">Email</label>
<%= text_field_tag :start_date,
params[:start_date],
placeholder: 'Kliknij, aby wybrać datę',
class: 'form-control date',
:data => {
provide: 'datepicker'
}
%>
</div>

You have kept your format as dd-mm-yyyy, but your param :start_date holds value in wrong order. If you want to set come default date, I will suggest you to use defaultViewDate option. Your calendar dates (which opens up initially) are disabled, that's why it selects some 1911 year date. try using startDate and endDate option. I hope it will fix it.

Related

Date Picker control not working as expected for 'dd-mm-yyyy' format in MS Edge

I am using the Datepicker via bootstrap JavaScript. I have this code:
<div class="col-sm-4">
#Html.TextBoxFor(m => m.Trust.EstablishmentDate, "{0:dd-MM-yyyy}", new { id = "establishmentDate", #readonly = "readonly", #class = "datefield form-control" })
#Html.ValidationMessageFor(m => m.Trust.EstablishmentDate, null, new { #class = "help-block" })
</div>
$(document).ready(function () {
$("#establishmentDate").datepicker({
changeMonth: true,
changeYear: true,
format: 'dd-mm-yyyy',
});
});
The above code worked fine while publishing it in IE, displaying and recognizing the date in the required dd-mm-yyyy format from date picker.
But the same code is not working as expected in the Edge browser where the below error is getting prompted:
The required field must be a date.
It's recognizing the format as mm-dd-yyyy and hence the above error appears when it finds the month as invalid for the cases like 22-06-22, where as for cases like 02-06-22, its recognizes as 6th Feb.

I'm getting NaN value on my daterangepicker

there. I just updated my daterangepicker on my website.It's working really fine on fields for posting ( starts on / ends on ), but...In my profile, I had my date picker with value="{{$user->dob}}" which gaves me the date of birth of my profile. Now, I updated my datepicker from DOB with this line :
<input type="text" name="dob" id="dob" value="{{ $user->dob }}">
but I'm getting NaN at value and when it's open up it looks like that :
.
Here is my script
$(function() {
$('input[name="dob"]').daterangepicker({
singleDatePicker: true,
showDropdowns: true,
minYear: 1901,
maxYear: 2099,
}, function(start, end, label) {
});
});
I'm using Daterangepicker : http://www.daterangepicker.com/
You should use the locale in your settings as described here:
http://www.daterangepicker.com/#example2
Because you need to specify in which format your date is passed to the component

DateTimePicker not updating values on ruby side

I am having an issue where the regular datepicker updates the value on the rails side in this format '2017-09-12 22:49:27', but I am trying to use datetimepicker and its not updating the value, it updates the text field with the format: M/DD/YYYY but not the actual value in the param as I need that in '2017-09-12 22:49:27' format but its staying the same value which is currently MM/DD/YYYY as what's loaded up for the text field and I am getting an undefined value when I try to see the current value:
var selectedDate = $("#from").find(".active").data("day");
Here is the ruby code
= text_field_tag :from_js, l((params[:from].to_datetime or #item.created_at), format: :american_no_time), class: 'form-control input-sm datetimepicker', id: 'from'
= hidden_field_tag :from, l((params[:from].to_datetime or #item.created_at), format: :american_no_time)
= text_field_tag :to_js, l((params[:to].to_datetime or Time.zone.now.to_datetime), format: :american_no_time), class: 'form-control input-sm datetimepicker', id: 'to'
= hidden_field_tag :to, l((params[:to].to_datetime or Time.zone.now.to_datetime), format: :american_no_time)
and here is the current javascript:
var maxDate = new Date();
var minDate = $("#from").val();
$(function () {
$('#from').datetimepicker({
format: "M/D/YYYY",
minDate: minDate,
maxDate: maxDate,
pickTime: false
});
$('#to').datetimepicker({
format: "M/D/YYYY",
minDate: minDate,
maxDate: maxDate,
pickTime: false
});
});
Any ideas or suggestions would be appreciated

how to resolve date picker format in mvc5

Im working in MVC5 entity framework. Here some fields have date fields so I need to set data format. Here I using script and datepicker. Now I got a problem if I set after date of 12. I got error like "enter valid date"
Script used in views:-
$('#datepick').datepicker({
dateFormat: 'dd/mm/yy', minDate: 0
});
Code used in views
<div class="row form-group">
<div class="col-md-4">
Due Date:
</div>
<div class="col-md-8">
#Html.TextBoxFor(model => model.goodreceipt.Due_Date, new { #Value = DateTime.Now.ToString("MM/dd/yyyy"), #class = "form-control control-text ", #id = "datepick" })
#*#Html.ValidationMessageFor(model => model.goodreceipt.Due_Date)*#
</div>
</div>
I got error Like this"Enter valid date" the date must be with in 12, after 12 (i.e 13-12-2014) error will occur so onlyIi comment validation message but here I itself I got this error.
Here i want to finish this task.
I guess there are different format of date..need some change as below and try again :-
$('#datepick').datepicker({ dateFormat: 'MM/dd/yyyy', minDate: 0 });

setting minDate in datepicker in Rails

I need to apply minDate attribute to the datepicker in Rails App as today's date.
In _admin_controls.html.erb
<%= f.input :end_date, wrapper: :append do %>
<div id="datepicker" class="datepicker input-group edit-left">
<%= f.text_field :end_date, :class => "datetime form-control" %>
application.js contains
jQuery(document).on('focus', 'input.datetime', function() {
opts = {format: 'M dd, yyyy', autoclose: true};
jQuery(this).datepicker(opts);
jQuery(".datepicker").css("z-index",10000);
});
What should be the javascript to do so only for _admin_controls.html.erb?
If You are using Jquery then you can set the date like this :
// If you want to set a date then assign it to a variable or create a function and call that function directly.
var currentDate = new Date();
$("#mydate").datepicker().datepicker("setDate", new Date());
I have been able to solve the problem.
In application.js, I removed the code related to datepicker and its librariies and created a new file datepicker.js
datepicker.js contains
/* bootstrap datepicker */
//= require bootstrap-datepicker/core
//= require bootstrap-datepicker/locales/bootstrap-datepicker.es
jQuery(document).on('focus', 'input.datetime', function() {
opts = {format: 'M dd, yyyy', autoclose: true};
jQuery(this).datepicker(opts);
});
In application.js
//= require datepicker
In _admin_controls.html.erb,
<%= f.input :end_date, wrapper: :append do %>
<div id="datepicker" class="datepicker input-group edit-left">
<%= f.text_field :end_date, :class => "datetime form-control", :'data-date-start-date' => Date.current.strftime('%m/%d/%Y') %>
And this disables backdate selection for particularly _admin_controls datepicker. :)
This should do it
$('#start_date').datepicker({
minDate: 0,
onSelect: function (selected) {
$("#end_date").datepicker("option", "minDate", selected)
}
});
Where end date being the one in the other box which allows to select the max one .
You can set the rails input field to the following:
<%= f.datetime_local_field(:end_date, min: Time.now().to_s.slice(0,10) ) %>

Categories

Resources