Bootstrap-calendar timezone issue - javascript

Overview
Users need to select a date in a form field. This form field uses bootstrap-datepicker. It is very important that dates before today are non selectable.
Code
var date = new Date();
$('.datepicker').datepicker({
todayHighlight: true,
format: 'yyyy-mm-dd',
startDate: date,
minDate: date,
});
Problem
It is the 15th-18:00H here at the moment. My timezone is Paris +0200H... if I change my computer time to +10 then I am the 16th-04:00H. Trouble is that the date picker is still showing the 15th... I need to some how account for the time-zone offset so that it shows the 16th.
I am also using jstz and moment.js
Note to future self:
To get this to work you need to do the following:
var date = new Date();
var startDate = moment().utcOffset(-this.date.getTimezoneOffset()).format('YYYY-MM-DD'); // get the users timezone offset
$('.datepicker').datepicker({
todayHighlight: true,
format: 'yyyy-mm-dd',
startDate: startDate, // pass it in
minDate: date,
});
$('input[name=start_date]').val(startDate); // set the value of the field

Related

JQuery datetimepicker exception day with minDate and maxDate

I'm facing a issue with datetimepicker.
I have a page with an Order, that has a delivery date.
Whenever the Order is listed, the datetimepicker has a certain value (for example, some day last week.) I want to be able to edit it but only select a set of avaiable dates (from today up to 30 days including "some day last week").
How should I proceed?
Function of the picker:
var maxdate = new Date();
maxdate.setDate(maxdate.getDate() + 30);
var $p = jQuery.noConflict();
$p("#foo").datetimepicker({
format: 'YYYY/MM/DD',
useCurrent:true,
showClose:true,
defaultDate: $.now(),
minDate: moment().millisecond(0).second(0).minute(0).hour(0),
maxDate:maxdate,
});
EDIT:
I'm guessing i wasn't very explicit with my question.
Example for what i want:
minDate = 2017/10/14
maxDate = 2017/10/30
(at this point everything works fine)
Date option that i want to pick = 2017/09/10 (that is 1 month older than minDate) without changing minDate!
I want to create an exception date that is not inside the range of min/max date.
For the options showed I guess you are using eonasdan-datetimepicker.
So proceed like this:
var maxDate = moment().add(30, 'day');
var $p = jQuery.noConflict();
$p("#foo").datetimepicker({
format: 'YYYY/MM/DD',
useCurrent: true,
showClose: true,
defaultDate: $p.now(),
minDate: moment().startOf('day'),
maxDate: maxDate,
});
Where maxDate will have 30 days added using moment method .add()
for minDate It is easier if you use startOf('day') as will be the same you have, but easier to read.
EDIT
Ok so what you want is to allow your users to choose a "special day" that is not in the array, so for that you could use enabledDates option.
In which you will add your special day and the range of the days enabled, so only thus will be allowed to be selected and would be something like this:
let currentFormat = 'YYYY/MM/DD';
let specialDay = '2017/09/10';
let maxDate = moment().add(30, 'day'); //To the picker not allow to go further in the view
let startDate = moment().startOf('day').format(currentFormat); //First date being added, current day
let enabledDates = [
moment(specialDay, currentFormat), //this is our "special day"
moment(startDate, currentFormat) // we format the date to the format needed ];
//Iterate and add 30 days, and only those will be able to be picked
for (var i = 1; i <= 30; i++) {
//apply the format
let date = moment().add(i, 'day').format(currentFormat);
enabledDates.push(moment(date, currentFormat));
}
//We init our picker with the 30 days and our special date
//`minDate` and `maxDate` still there to give the style to the view
$("#myDatepicker").datetimepicker({
format: currentFormat,
useCurrent: false,
showClose: true,
minDate: moment(specialDay, currentFormat),
maxDate: maxDate,
enabledDates: enabledDates,
});
Working fiddle https://jsfiddle.net/William_/2ze7bo27/
Edit: Make easier to change format and special date

Pikaday calendar date format not working

I'm using the Pikaday calendar library but the date format seems to be being ignored. I have this code:
<script>
var picker = new Pikaday(
{
field: document.getElementById('dayDate'),
firstDay: 1,
minDate: new Date(2017, 0, 1),
maxDate: new Date(2020, 12, 31),
yearRange: [2017, 2020],
format: 'YYYY-MM-DD',
bound: false,
container: document.getElementById('date-container'),
});
</script>
This outputs the date in this format 'Thu May 25 2017' I need it to be in the format '2017-05-25'. What am I doing wrong?
Based on the Pikaday documentation, you have to reference Moments.js to make the custom date formatting work.
From the docs:
format: the default output format for .toString() and field value (requires Moment.js for custom formatting)
Make sure to reference the Moments.js library before Pikaday.

How can I correctly set the default value for bootstrap 3 datetimepicker?

I am trying to set the default date/time for a bootstrap 3 datetimepicker
to today's date 08:00 AM.
The following code is what I have done. However, that does not seems to be setting the datetime for the field.
$(function(){
$('#started-at').datetimepicker({
format: 'MM/DD/YYYY LT',
useCurrent: false,
defaultDate: getDefaultStartAt()
});
});
function getDefaultStartAt()
{
var d = new Date();
var month = d.getMonth();
var day = d.getDate();
var year = d.getFullYear();
return new Date(year, month, day, 8, 0);
}
How can I correctly set the default value for bootstrap-datetimepicker?
Your js code is fine.
You probably didn't include (or in wrong order) something from minimal requirements. moment.js maybe?
Or your html could be wrong.
Here you can see working example with your code: https://jsfiddle.net/5vv9chgx/

How to move current date to the first row of the jQuery datepicker calendar

Is that possible to shift current day to the first row of the datepicker calendar? So the first row always contains the current day.
Following code will set minDate as First date of current month and maxdate as last date of current month.
var currentTime = new Date();
// First Date Of the month
var startDateFrom = new Date(currentTime.getFullYear(),currentTime.getMonth(),1);
// Last Date Of the Month
var startDateTo = new Date(currentTime.getFullYear(),currentTime.getMonth() +1,0);
$("#datepicker").datepicker({
dateFormat: 'dd.mm.yy',
minDate: startDateFrom,
maxDate: startDateTo
});

Javascript Date format not working with GetDay

I am using a jquery datepicker and i had an option box being populated with info depending on what day it was..
Worked perfectly...
$('#bydate').datepicker({
showOn: "button",
buttonImage: "images/calendar.gif",
buttonImageOnly: true,
beforeShowDay: unavailable,
minDate: -0,
dateFormat: "dd/mm/yy",
onSelect: function(e) {
var date = new Date(e);
var day = date.getDay(); // 0 = sunday etc...
// clean all the options
$("#duration").empty();
// if monday
if (day === 1) {
// add "5 days" options
$("#duration").append("<option value='5'>5 days</option>");
// else if friday
} else if (day === 5) {
// add 3 / 7 / 14 days options
$("#duration").append("<option value='3'>3 days</option>"
+ "<option value='7'>7 days</option>"
+ "<option value='14'>14 days</option>");
} else { // else...
}
}
Until i came to needing to change the format from mm/dd/yy to dd/mm/yy.
Now it doesn't work, it looks like getDay is getting the month number and trying to calculate the day number...
I need date to know its dd/mm/yy or getDay to know that I am using dd/mm/yy
example here http://offline.raileisure.com/
The problem is that you changed the format of how the datepicker shows the dates, but not the one of the constructor Date(). You can do this:
onSelect: function(e) {
e = e.split('/')[1] + '/' + e.split('/')[0] + '/' + e.split('/')[2];
var date = new Date(e);
...
You can also use the datepicker's parseDate function, which takes the date format as parameter. See http://docs.jquery.com/UI/Datepicker/parseDate for details

Categories

Resources