How to set default date as 1 of month in datepicker? - javascript

I'm using http://www.eyecon.ro/bootstrap-datepicker/. I am trying to set default day as 1 of month.
<script>
var firstDayOfMonth = function() {
// your special logic...
return 1;
};
var d = new Date();
var currMonth = d.getMonth();
var currYear = d.getFullYear();
var startDate = new Date(currYear,currMonth,firstDayOfMonth());
console.log(startDate);
$('#dp1').datepicker('setDate',startDate);
</script>
But it doesnt work as expected.Instead it comes with today's date.What i am doing wrong?

Related

Primefaces Calendar setDate not working properly

I'm facing an issue when trying to set the date and time of Calendar in primefaces 6.1 using javascript. I used the following code and at first, it was working but suddenly it stopped working. Now I can't figure out how to fix this thing. Sometimes it changes the date part but not the time part.
var recievedDateValue = PF('recievedDate').getDate();
PF('collectedDate').setDate(recievedDateValue);
XHTML Part:
<p:calendar id="caseCollectedDate" widgetVar="collectedDate" tabindex="50" value="#{caseController.selectedCase.caseCollectedDate}"
converter="DateConverter"
converterMessage="Please enter valid date time format" pattern="MM/dd/yy HH:mm" maxdate="#{viewUtilityBean.currentDate}"
timeControlType="slider" onfocus="getFocusElementId()" onchange="validateDate('collectedDate')">
<p:remoteCommand name="validateCollectedDateFromServer" update="#this"/>
</p:calendar>
DateConverter (getAsObject method):
if (!value.contains(":")) {
date1 = new SimpleDateFormat("MM/dd/yy").parse(value);
value = new SimpleDateFormat("MM/dd/yy HH:mm").format(date1.getTime());
date1 = new SimpleDateFormat("MM/dd/yy HH:mm").parse(value);
} else {
date1 = new SimpleDateFormat("MM/dd/yy HH:mm").parse(value);
}
I tried multiple ways to somehow work this but it is not working. Though setdate function calls the onchange event but the date doesn't change.
I overrode the setDate function of the primefaces calendar widget to the following.
The following are the snippets I tried.
setDate : function(a) {
var month = a.getMonth();
var date = a.getDate();
var year = a.getFullYear();
var hour = a.getHours();
var min = a.getMinutes();
this.jqEl.datetimepicker("setDate",new Date(month,date,year,hour,min))
}
setDate : function(a) {
var month = a.getMonth();
var date = a.getDate();
var year = a.getFullYear();
var hour = a.getHours();
var min = a.getMinutes();
this.jqEl.datetimepicker({
userCurrent: false,
format: 'MM-dd-yy HH:mm',
date: new Date(month,date,year,hour,min)
})
}
setDate : function(a) {
var month = a.getMonth();
var date = a.getDate();
var year = a.getFullYear();
var hour = a.getHours();
var min = a.getMinutes();
this.jqEl.datetimepicker({
date: new Date(month,date,year,hour,min)
})
}
setDate : function(a) {
var month = a.getMonth();
var date = a.getDate();
var year = a.getFullYear();
var onlyDate = month+"/"+date+"/"+year;
var hour = a.getHours();
var min = a.getMinutes();
this.jqEl.datetimepicker({
date: onlyDate,
hour: hour,
minute: min
})
}
setDate : function(a) {
$('#clientTabs\\:j_idt1125\\:caseCollectedDate_input').data("DateTimePicker").date(a);
}
Calendar looks like this.

the function is not alerting something

using this code to check if the user is over 18 years age or not. but the function is alerting only "you are not 18+". also there is no error in console
for ex:- 23/12/1500 23/12/2008
var dob = document.getElementById("dob").value;
var jsdate = new Date(dob);
var jsdatearray = jsdate.toString().split("/");
var day = jsdatearray[0];
var month = jsdatearray[1];
var year = jsdatearray[2];
var nowdate = new Date();
nowdate.setFullYear(year, month - 1, day);
var maxDate = new Date();
maxDate.setYear(maxDate.getYear() - 18);
if (maxDate < nowdate) {
alert('you are 18+');
}
else {
alert('you are not 18+');
}
There were a few things off in the code. First, the date wasn't correctly imported into the Javascript. Second, the condition checking the date was backward. The code below should work better.
var dob = document.getElementById("dob").value;
var jsdatearray = dob.split("-");
var year = jsdatearray[0];
var day = jsdatearray[1];
var month = jsdatearray[2];
var nowdate = new Date();
nowdate.setFullYear(year, month - 1, day);
var maxDate = new Date();
maxDate.setYear(maxDate.getYear() - 18);
console.log(maxDate < nowdate);
if (maxDate > nowdate) {
alert('you are 18+');
}
else {
alert('you are not 18+');
}

datetime picker, check is today

I'm fetching users input in jquery datetime picker in format like this
2017-02-07 10:05
which is fine. My question is: how can I check is this users input today or not?
After getting suggestion from this question this is how you can do that. First setting the date constructor for today and the test date then set their time portion equal to zero and compare.
There are also other cool plugins to achieve the same more easily and more accurately like dateJs , momentJs
Using momentJs
var inputDate = new Date("2017-02-07 10:05");
var isToday = inputDate.isSame(new Date(), "day");
Using dateJs
var isToday = Date.equals(Date.today(), new Date("2017-02-07 10:05").clearTime());
Using native JavaScript date
var inputDate = new Date("2017-02-07 10:05");
var todaysDate = new Date();
var isToday = (inputDate.setHours(0, 0, 0, 0) == todaysDate.setHours(0, 0, 0, 0));
console.log(isToday);
Simplest Way...
var date = new Date('2017-02-07 10:05');
var now = new Date();
console.log(now.toString().substring(4, 15) == date.toString().substring(4, 15));
How about something like this:
<html>
<head>
</head>
<body>
<script>
var dateString = "2017-02-09 10:05";
var dateObj = new Date(dateString);
var ddSomeDate = dateObj.getDate();
var mmSomeDate = dateObj.getMonth()+1; //January is 0!
var yyyySomeDate = dateObj.getFullYear();
var dateObjToday = new Date();
var ddCurrentDate = dateObjToday.getDate();
var mmCurrentDate = dateObjToday.getMonth()+1; //January is 0!
var yyyyCurrentDate = dateObjToday.getFullYear();
if (ddSomeDate == ddCurrentDate &&
mmSomeDate == mmCurrentDate &&
yyyySomeDate == yyyyCurrentDate)
console.log("Same day");
else
console.log("NOT same day");
</script>
</body>
</html>
Try this is used to check the date is today or not
Here is Jquery code
$('#dp').datepicker({
onSelect: function(dateText) {
var today = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()).getTime();
var selected = new Date(dateText).getTime();
if (today > selected) alert('prior to today');
else if (today < selected) alert('after today');
else alert('today');
}
});​
And My html code
<input id="dp"/>
Demo here
http://jsfiddle.net/j08691/yBDVJ/
You must parse the date and use this isDateToday function to check:
var date = new Date('2017-02-07 10:05');
var isToday = isDateToday(date);
function isDateToday(td){
var d = new Date();
return td.getDate() == d.getDate() && td.getMonth() == d.getMonth() && td.getFullYear() == d.getFullYear();
}
you can get the current Date-Month-year by:
var d = new Date();
var date = d.getDate();
var month = d.getMonth();
var year = d.getFullYear();
then match the user selected date, month and year by these values.
Try something like this:
var today = new Date();
today.setHours(0);
today.setMinutes(0);
today.setSeconds(0);
if (Date.parse(today) == Date.parse(selectedDate)) {
alert('today!');
} else {
alert('not today');
}

Date function in JS, comparing with a set date not working

I have to pretty much validate and check if the user is underage in ASP. So anyone under the age of 01/07/1998 is underage. I have 3 dropdown lists to select date, month and year. Im using JS. I try using concat and split but they dont work. Can anyone help with this?
function ValidateDate() {
var dDay = document.getElementById('DateList');
var dMonth = document.getElementById('MonthList');
var dYear = document.getElementById('YearList');
var day = dDay.selectedIndex;
var month = dMonth.selectedIndex;
var year = dYear.selectedIndex;
var firstValue = year + month + day;
var setyear = "1998";
var setmonth = "06";
var setdate = "01";
var secondValue = setyear + setmonth + setdate;
var firstDate = new Date();
firstDate.setFullYear(firstValue[0], (firstValue[1] - 1), firstValue[2]);
var secondDate = new Date();
secondDate.setFullYear(secondValue[0], (secondValue[1] - 1), secondValue[2]);
if (firstDate > secondDate) {
alert("Pass");
}
else {
alert("Fail");
}
}
<asp:CustomValidator ID="CustomValidatorDate" runat="server"
ErrorMessage=" You are underage" CssClass="error" Display="Dynamic" ClientValidationFunction="ValidateDate" ></asp:CustomValidator>
First of all your day, month and year variables are reading the index of the drop-downs and not their values.
For instance, you need to use:
var day = dDay.value,
month = dMonth.value,
year = dYear.value;
And then create date object as
var firstDate = new Date(year, month, day);
-Dipen

Check if a date within in range

I am trying to check if a date of format mm.dd.yyyy is greater than today and less than the date after 6 months from today.
Here is my code:
var isLinkExpiryDateWithinRange = function(value) {
var monthfield = value.split('.')[0];
var dayfield = value.split('.')[1];
var yearfield = value.split('.')[2];
var inputDate = new Date(yearfield, monthfield - 1, dayfield);
var today = new Date();
today = new Date(today.getFullYear(), today.getMonth(), today.getDate());
alert(inputDate > today);//alert-> true
var endDate = today;
endDate.setMonth(endDate.getMonth() + 6);
alert(inputDate > today);//alert-> false
if(inputDate > today && inputDate < endDate) {
alert('1');
} else {
alert('2');/always alert it
}
}
If I execute isLinkExpiryDateWithinRange('12.08.2012') I wish it will show 1 as this is within the range, but it is displaying 2. Moreover the first alert is showing true and the second one false.
Can anyone please explain what is happening?
Change:
var endDate = today;
to:
var endDate = new Date(today);
See the posts here for how objects are referenced and changed. There are some really good examples that help explain the issue, notably:
Instead, the situation is that the item passed in is passed by value.
But the item that is passed by value is itself a reference.
JSFiddle example
function isLinkExpiryDateWithinRange( value ) {
// format: mm.dd.yyyy;
value = value.split(".");
var todayDate = new Date(),
endDate = new Date( todayDate.getFullYear(), todayDate.getMonth() + 6, todayDate.getDate() +1 );
date = new Date(value[2], value[0]-1, value[1]);
return todayDate < date && date < endDate;
}
isLinkExpiryDateWithinRange("12.24.2012"); // true
isLinkExpiryDateWithinRange("12.24.2020"); // false
Below function checks if date selected is within 5 days from today. Date format used is "DD-MM-YYYY", you can use any format by changing value.split('-')[1] order and split character.
function showMessage() {
var value = document.getElementById("invoiceDueDate").value;
var inputDate = new Date(value.split('-')[2], value.split('-')[1] - 1, value.split('-')[0]);
var endDate = new Date();
endDate.setDate(endDate.getDate() + 5);// adding 5 days from today
if(inputDate < endDate) {
alert("If the due date selected for the invoice is within 5 days, and express settlement fee will apply to this transaction.");
}
}

Categories

Resources