Datepicker date is not being set through jQuery code - javascript

Hello all I am trying to set date of datepicker control in jQuery. When I run the page through Mozilla Firefox browser it works fine. However, when I run it in Google Chrome the date is not being set. Does anybody know what the problem is?
cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
var myDate = new Date(cookie_value);
var date1 = new Date(Date.parse(myDate));
date1.setDate(date1.getDate());
var newDate = date1.toDateString();
newDate = new Date(Date.parse(newDate));
var option = "minDate";
$("#ctl00_ContentPlaceHolder1_txtArrivalDate").datepicker("option", option, newDate);
// $("#ctl00_ContentPlaceHolder1_txtArrivalDate").datepicker("option", option, newDate);
$("#ctl00_ContentPlaceHolder1_txtDepartureDate").datepicker('setDate', newDate);

Much of your code seems redundant. Why do date1=new Date(....) and then date1.setDate(...)? Why create newDate if it is just a copy of date1?
See:
http://jsfiddle.net/QB6K6/
It works in chrome for me using the code below. Depends on the value of your cookie, though.
$(document).ready(function () {
var cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
var myDate = new Date(cookie_value);
$("#ctl00_ContentPlaceHolder1_txtArrivalDate").datepicker( {minDate:myDate});
$("#ctl00_ContentPlaceHolder1_txtDepartureDate").datepicker();
$("#ctl00_ContentPlaceHolder1_txtDepartureDate").datepicker("setDate", myDate);
});

Related

Default date range with lightpicker.js and moment.js

How can I get the following codepen to default to showing all the data. I currently defaults to 21/11/2019 - 28/11/2019. If I change the range to 01/11/2019 - 30/11/2019 it shows all the data. I'm using lightpicker.js and moment.js. Thank you.
UPDATE: What I'm trying to do is show all the data with nothing in the Date Range. Just page defaulting to show everything with no filters. Then I should be able to filter if I choose dates in the Date Range. When I comment out the line picker.setDateRange(new Date(), moment().add(7, 'day')); It show blank text box for the Date Range but no data is shown.
JS
var picker = new Lightpick({
field: document.getElementById('datepickerA'),
singleDate: false
});
picker.setDateRange(new Date(), moment().add(7, 'day'));
$scope.onOkA = function(){
var startDate = picker.getStartDate().format('MM/DD/YYYY')
var endDate = picker.getEndDate().format('MM/DD/YYYY')
if(startDate && endDate){
$scope.filteredTicketA = $scope.ticketsA.filter(ele=>{
return (new Date(ele.Date)-new Date(startDate)>=0) && (new Date(ele.Date)-new Date(endDate)<=0)
})
}
}
new Date() will default to today, if you want it be 01/11/2019 then use new Date(2019,11-1, 1).
However I would use some constants to make code clearer, replace picker.setDateRange() sentence with these 3 lines:
var startDate = new Date(2019,11-1, 1); // be aware that month is 0 based.
var endDate = moment(startDate).endOf('month');
picker.setDateRange(startDate, endDate);
as a quick solution for onOKA(){} I would change this:
var defaultStartDate = moment(new Date(1, 10, 2019));
var startDate = (pickerB.getStartDate() || defaultStartDate).format('MM/DD/YYYY');

Populate fields with dates JS/JQ

So I have this line:
<input type="date" id="Date1" name='date' data-role='datebox'
data-options='{"dateFormat": "mm/dd/YYYY"}' />
Works as it should, I'm using this.
When I click on that <input> a datepicker will come up and and the user will be able to choose a date with the mm/dd/yyyy format.
However, I want to populate that field, before, let's say onload, window ready, I don't even care, with the date of today.
I'm trying this:
<script type="text/javascript">
today = new Date();
var dateString = today.format("dd/mm/yyyy");
document.getElementById("Date1").innerHTML = dateString;
</script>
And I basically know where the problem is, I'm trying to add a string into a type="date" field; I've also tried, instead of using .innerHTML, .value but still no luck, Not sure how to do it.
Also, keep in mind that I have 3 more fields that have to be populated.
1: - the one that I've already pasted >> current date 2: - current
date +19 weeks 3: - current date +72 weeks 4: - current date +1 week
I don't mind using JQ if needed.
This works:
document.getElementById('Date1').valueAsDate = new Date();
Refer the API here
Your datepicker seems to be able to do what you want: RFTM darn it. It also seems your are not using a regular date input (inspect your date widget you'll see that it is actually a text input). My guess is that your datepicker transforms the date input in a text input when it's initialized.
I actually fixed it with patience and readng a lot.
Here's the code.
This to populate the fields when you open the document:
<script type="text/javascript">
today = new Date();
var dateString = today.format("dd/mm/yyyy");
document.getElementById("Date1").value = dateString;
var dateString18 = new Date(+new Date + 10886400000);
dateString18 = dateString18.format("dd/mm/yyyy");
document.getElementById("Date2").value = dateString18;
var dateString90 = new Date(+new Date + 54432000000);
dateString90 = dateString90.format("dd/mm/yyyy");
document.getElementById("Date3").value = dateString90;
var dateString1 = new Date(+new Date + 604800000);
dateString1 = dateString1.format("dd/mm/yyyy");
document.getElementById("Date4").value = dateString1;
</script>
And this is to calculate the other fields after I change the first one:
function dateChange(fecha) {
var anyo = fecha.substring(0, 4);
var mes = fecha.substring(5, 7);
var dia = fecha.substring(8, 10);
var fechaFormato = dia + "/" + mes + "/" + anyo;
document.getElementById("Date1").value = fechaFormato;
var strDate = fechaFormato;
var dateParts = strDate.split("/");
var dateBuena = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
var dateString18 = new Date(+dateBuena + 10886400000);
dateString18 = dateString18.format("dd/mm/yyyy");
document.getElementById("Date2").value = dateString18;
var dateString90 = new Date(+dateBuena + 54432000000);
dateString90 = dateString90.format("dd/mm/yyyy");
document.getElementById("Date3").value = dateString90;
var dateString1 = new Date(+dateBuena + 604800000);
dateString1 = dateString1.format("dd/mm/yyyy");
document.getElementById("Date4").value = dateString1;
}

moment.js - isBetween() method doesn't work consistently

I have a moment.js code that works well:
var startDate = '2015-05-06T19:00:00+0300';
moment(startDate).isBetween(moment(), moment().add(30, 'days'));
// returns true, that's great!
But when I start refactoring to make it more readable it fails to work:
var today = moment();
var startDate = '2015-05-06T19:00:00+0300';
moment(startDate).isBetween(today, today.add(30, 'days'));
// returns false.. but why?
var today = moment();
var startDate = '2015-05-06T19:00:00+0300';
moment(startDate).isBetween(today, moment(today).add(30, 'days'));
You are passing a reference which you have edited by adding 30 days.

new Date is not working on Firefox

Can you tell me why this is not working on Firefox (V 34 latest) ? It's working fine on all other browsers. 'DatePosted' is shown as Invalid Date.Why ? Any help would be highly appreciated.
//Get local time for everything:
data.Comments.forEach(function (x) {
x.DatePosted = new Date(x.DatePosted.toString().replace("T", " ") + " UTC");
});
Note : x.DatePosted : "2014-11-18T08:06:39.06"
You dont need to replace the T. It works without it (tested in Chrome and Firefox).
After setting the Date object, get it into UTC.
Working snippet below:
var myDate = new Date("2014-11-18T08:06:39.06");
// now set it to UTC
var myDateinUTC = Date.UTC(myDate.getFullYear(), myDate.getMonth(), myDate.getDate(), myDate.getHours(), myDate.getMinutes(), myDate.getSeconds(), myDate.getMilliseconds());
console.dir(myDateinUTC);
var myNewDate = new Date(myDateinUTC);
console.log(myNewDate.getMonth()); // just to test

How to set the ios 5 HTML5 input of type="date" to the current date & time?

I tried the obvious and does not work...
var dtel = document.getElementById("element-id");
dtel.value = new Date();
It needs a string but what is the syntax?
After many trials and errors I found that the following works:
var dtel = document.getElementById("element-id");
var dt = new Date();
dtel.value = dt.toISOString();

Categories