bootstrap-datetimepicker hours and minutes not defaulted to 00:00 - javascript

how to set default time hours and minutes as 00:00 but if i pick hours and minutes picked value should come. My issue is current time is coming by default and current date is highlighting. i am using bootstrap date time picker
By using option i am changing like this but not working. if i change format like dd/mm/yyyy 00:00 all the time it is showing 00:00 even if i pick hours and minutes. pls help me to solve this issue
My code is
$(document).ready(function(){
$('#datetimepicker1').datetimepicker({
$('#datetimepicker1').datetimepicker("setHours",'00' );
});
$('#datetimepicker2').datetimepicker({
$('#datetimepicker2').datetimepicker("setMinutes",'00' );
});

Perhaps you can try initializing the date picker with a default date, instead of trying to explicitly set the values. (I'm not sure datetimepicker even has setHours/minutes methods.) For example, you might do something like:
var d = new Date();
// hours, minutes, seconds
d.setHours(0, 0, 0);
$('#datetimepicker1').datetimepicker({
defaultDate: d
});
Hopefully this works for you or at least points you in the right direction! :)

What's jquery you using? Is it here ?
You should try to use DateTimePicker jQuery. It's simple and very good.
jQuery('#datetimepicker').datetimepicker({
format : 'd/m/Y H:i',
defaultTime:'00:00',
formatTime:'H:i'
});

Related

Date calculation problem due to change of year in momentJS

I have used moment.js to calculate 30 days limit in a form field where I have used daterangepicker.js to pick the date. I have disabled all previous date from current date and all dates after 30 days counting from current date. Everything was working fine until the "year" nation attacked(hehe git it?).
My date calculation was working fine using moment.js, but as the year is changing, everything breaks. Whenever I change the year to 2021 from 2020 every date becomes disabled. Like I want to post something today(22/12/2020) and want to set the deadline next year, to do that when I change the year every date becomes disable. Before changing the year I can see the dates of next year enabled and I can select it, it works fine like that. But whenever I change the year dates becomes disabled.
This is my current code to calculate date limit:
$('input[name="application_deadline"]').daterangepicker({
locale: {
format: 'DD-MM-YYYY HH:mm'
},
singleDatePicker: true,
showDropdowns: true,
startDate: moment(),
endDate: moment().subtract(-29, 'days'),
minDate: moment(),
maxDate: moment().subtract(-29, 'days')
});
My code technically works if I disable the line:
maxDate: moment().subtract(-29, 'days')
Then every date is enabled after the current date but removes the 30 days limit which I require to be working.
I have attached two screenshots of before and after changing year for the reference.
What you're describing sounds like a bug in daterangepicker.js. To reproduce, I've tested latest version from their website using jQuery 3.5.1.
Unfortunately I'm not able to reproduce what you're describing. Did you try updating to latest version 3.1?

How to set specific timezone in datepicker using jquery only?

I've a field and applying datepicker on it using jQuery.
it is currently getting time from system/browser.
I want it to get time from specific time zone e.g America/new_york.
The endDate param is the key to set the calendar, means user should not be able to select the date from future. Currently it is looking like this
The code snippet is :
jQuery('#convo_start_date').datepicker({
format: 'dd M yyyy',
endDate: '+0d',
autoclose: true,
showButtonPanel: true,
todayBtn: 'linked'
}).on('change', function () {
jQuery('.datepicker').hide();
jQuery('#convo_end_date').attr('value',jQuery('#convo_start_date').val());
});
Question: Is there any way to set the default specific timezone like America/new_york to do not allow the date from future (according to this specific timezone)?
Note: I've tried moment.js but it is conflicting with my current work in jQuery, Is there any params datepicker library providesvto set with timezone?
If you are using jquery ui datepicker plugin, you can set the maxDate option to current date so that user can't select date from future.
You will need to do the conversion to the specific timezone. You can change the targetTimeOffset variable as per your requirement.
var d = new Date();
var targetTimeOffset = -4*60; //desired time zone, taken as GMT-4
d.setMinutes(d.getMinutes() + d.getTimezoneOffset() + targetTimeOffset );
Check Fiddle: http://jsfiddle.net/mpsingh2003/8w8v9/3387/ if this is what you are looking for
You can use Joda-Time library to get your solution.
Check the classes they provide like org.joda.time.DateTimeZone.
You can get the DateTimeZone depending on the Canonical ID defined in the Joda Time.
Please check this link for API documentation.
Hope this will helpful for you. Thanks.

jQuery datetimepicker default time is not working

I am using jquery datetimepicker. I used minDate to set the minimum date, a user can select. Now i want to make timepicker independent. So I am using hour and minute to set the default time, but its not working. Here is my code:
jQuery('#datetimepicker1').datetimepicker({
minDate: "06/20/2017",
hour : '06',
minute: '00'
});
It give me error:
Uncaught TypeError: option hour is not recognized!
Untested code, let me know if there is any problem:
jQuery('#datetimepicker1').datetimepicker({
minDate: "06/20/2017"
});
var myDate = new Date();
myDate.setHours(6);
myDate.setMinutes(0);
myDate.setDate(20);
jQuery('#datetimepicker1').datetimepicker("setDate", myDate);

How to find today's date in DateTimePicker JavaScript?

I'm using the following javascript: http://xdsoft.net/jqplugins/datetimepicker , and I want to achieve simple effect - when User selects today's day, it should show him only hours available from now until the end of the day, the previous time should be disabled. But when he choses any other day in the future - then the whole time should be available. I wrote the following function in JS:
<script>
var today = new Date();
var dd = today.getDate();
alert(dd);
var logic = function( currentDateTime ){
if( currentDateTime.getDay()==dd ){
this.setOptions({
formatTime:'g:i A',
format: 'd/m/Y h:i A',
minDate:'+1970/01/02',//today
minTime: //I don't know yet how to implement the current time
});
}else
this.setOptions({
formatTime:'g:i A',
format: 'd/m/Y h:i A',
minDate:'+1970/01/02'//today
});
};
jQuery('#datetimepicker').datetimepicker({
onChangeDateTime:logic,
onShow:logic
});
</script>
The problem is that that line:
currentDateTime.getDay()==dd
doesn't work, because in my case dd equals todays day of the month (e.g. 25), and currentDateTime.getDay() checks current day of the week (e.g. for saturday it's 6). Is there anyone who could help me with that issue? I know there are some other available solutions (other datetime pickers), but I cannot find any other that is as simple and elegant as this. Thanks!
You want to use getDate() which returns the day in the month, instead of getDay() which returns the day in the week.
You should read the Date reference page.

How to avoid time zone issues with JQuery datepicker

I'm using datepicker in an input form, and sending the results through json to a database. I am using this line, to get the date from the datePicker:
date = $("#datepicker").datepicker('getDate');
Now, I would expect this to return 2014-04-03T00:00:00.000Z
But in fact it returns 2014-04-02T22:00:00.000Z
Notice the two hour difference, which unintentionally changes the day of month as well. I have no use for the hours and the smaller time units. However I do want the date to be right, without adding a dreaded +1 to my code. I suspect this has something to do with time zones, but I can't seem to find a solution to it in the documentation, or other Q&A's online. Could anyone point me in the right direction? My time zone is GMT +1 if that matters.
Thanks :)
I solved this a while ago, but forgot to post an answer.
After retrieving date, this is how i fixed it:
date.setMinutes(date.getMinutes() - date.getTimezoneOffset());
voilla
I could not figure out what you did there so I came up with a bit of a hackterrific solution.
I took the value of the alt field in UNIX:
$( function() {
$( "#datepicker" ).datepicker({
altField: "#alternate",
altFormat: "#",
});
It came out all sorts of weird with 3 extra 0's and a day behind my time zone.
So I figured out the difference and added it on.
var a = document.getElementById("alternate").value; // take alternative field UnixTimeStamp value
a = a.slice(0, -3); // get rid of 3 extra 0's
a = +a + +57000; // convert to Thai time

Categories

Resources