I'm creating a custom ASP.Net validator that checks if the data entered is a non working day.
The definition of "non working day" is:
The date is either a Saturday or Sunday
The date is a national holiday
I've coded the c# stuff and have created expando attributes for the error message and a string of holiday dates. Having done that, I've created a javascript function that contains this code to check for none working days. I've removed other code for brevity.
if (sender.NonWorkingDayErrorMessage && sender.Holidays) {
// Is weekend
if (date.getDay() == 6 || date.getDay() == 0) {
sender.innerHTML = sender.NonWorkingDayErrorMessage;
return;
}
// Is holiday
var holidays = sender.Holidays.split(";");
for (var i = 0; i < holidays.length; i++) {
var h = new Date(Date.parse(holidays[i]));
if (h === date) {
sender.innerHTML = sender.NonWorkingDayErrorMessage;
return;
}
}
}
The issue I have is that the code h === date is always false - here's the output I get when I add an alert and type in 26/8/2013.
Mon Aug 26 2013 00:00:00 GMT+0100 (GMT Standard Time) => Mon Aug 26 2013 00:00:00 GMT+0100 (GMT Standard Time) => false
As you can see I parse the holidays but I also test the input 'date' like this further up the function like this:
// Deconstruct string and reconstruct
// as date.
var parts = args.Value.split("/");
var day = parseInt(parts[0],10);
var month = parseInt(parts[1],10) -1;
var year = parseInt(parts[2],10);
var date = new Date(year, month, day);
// Valid date format but not a valid date
if (date.getFullYear() !== year || date.getMonth() !== month || date.getDate() !== day) {
sender.innerHTML = sender.InvalidErrorMessage;
return;
}
Anyone got ideas as to why these two dates aren't seen as matches?
Try like this
var a = new Date(2013,12,1);
var b = new Date(2013,12,1);
a.getTime() === b.getTime()
Related
I need to find out if two dates the user selects are the same in Javascript. The dates are passed to this function in a String ("xx/xx/xxxx").That is all the granularity I need.
Here is my code:
var valid = true;
var d1 = new Date($('#datein').val());
var d2 = new Date($('#dateout').val());
alert(d1+"\n"+d2);
if(d1 > d2) {
alert("Your check out date must be after your check in date.");
valid = false;
} else if(d1 == d2) {
alert("You cannot check out on the same day you check in.");
valid = false;
}
The javascript alert after converting the dates to objects looks like this:
Tue Jan 25 2011 00:00:00 GMT-0800 (Pacific Standard Time)
Tue Jan 25 2011 00:00:00 GMT-0800 (Pacific Standard Time)
The test to determine if date 1 is greater than date 2 works. But using the == or === operators do not change valid to false.
Use the getTime() method. It will check the numeric value of the date and it will work for both the greater than/less than checks as well as the equals checks.
EDIT:
if (d1.getTime() === d2.getTime())
If you don't want to call getTime() just try this:
(a >= b && a <= b)
var d1 = new Date($('#datein').val());
var d2 = new Date($('#dateout').val());
use two simple ways to check equality
if( d1.toString() === d2.toString())
if( +d1 === +d2)
var date = Wed Oct 07 2015 19:48:08 GMT+0200 (Central European Daylight Time);
var dateOne = new Date(date);
var dateTwo = new Date();
var isEqual = dateOne.getDate() === dateTwo.getDate()
this will give you the dates equality
I use a function to calculate if the day is a week-end or no, the function work, but not for december. What is wrong?
weekEnd: function(date) {
// know if week or week-end
var date1 = new Date(date);
var day = date1.getDay();
var resultat = 1;
if (day === 6 || day === 0) {
resultat = 0;
}
return resultat;
},
I can't reproduce the steps as it works for me. But instead of using 0 or 1, you can use boolean values true or false.
Maybe if you're having this issue, it's because the date format is the wrong one.
It looks like you're French (according to your comment) and maybe your Date object is set to use MM-DD-YYYY date format instead of our french format being DD-MM-YYYY
Here is my code and it works for me, even in december :
const isWeekend = (date) => {
const day = new Date(date).getDay()
return ( day == 6 || day == 0)
}
// 12-15-2019 --> Sunday 15th December 2019
console.log(isWeekend('12-15-2019')) // returns true
I have an object, Object { 2014-01-30=[1], 2014-02-01=[1]}
and an array called fechasPeriodo,
so why is the following code:
fechasPeriodo = [];
for(var property in SelectedDates) {
fechasPeriodo.push(new Date(property));
}
producing this result [Date {Wed Jan 29 2014 18:00:00 GMT-0600}, Date {Fri Jan 31 2014 18:00:00 GMT-0600}]
Edit: I'd expect the result to be Thu Jan 30 2014 etc. , Sun 02 Feb 2014.
Actually it's a problem only because I'm trying to define a range of dates in datepicker using a Google Calendar feed. So the following code:
if(fechasPeriodo.length > 1) {
r[1] = fechasPeriodo[0] <= date && date <= fechasPeriodo[1] ?"Highlighted"+SelectedDates[key][0].replace(/\s/g, "_"):"Highlighted-unknown";
}
I'd expect to highlight a range from the 30th Jan to the 02nd of February. But if you can guide me as to why it isn't working, I'd be very grateful, I'm following this fiddle:
http://jsfiddle.net/qaEuj/
At the risk of being downvoted again I have to say that I still don't understand why my last bit of code above isn't working like the fiddle I mention, so here's my complete code:
$(document).ready(function() {
var fechaDefecto = new Date('2014/01/01');
var fechaFin = new Date('2014/08/31');
SelectedDates = null;
/*SelectedDates[new Date('12/25/2014')] = new Date('12/25/2014');
SelectedDates[new Date('12/12/2014')] = new Date('12/12/2014');
SelectedDates[new Date('06/06/2014')] = new Date('06/06/2014');*/
$('#tiposFechas').change(function() {
$.getJSON("https://www.google.com/calendar/feeds/cide.edu_sm151i2pdhu2371vq8hamcver4#group.calendar.google.com/public/full?q="+encodeURI($(this).val()), {"alt" : "json"}, function(data) {
SelectedDates = {};
$.each(data.feed.entry, function(i, entry) {
var key = entry.gd$when[0].startTime.substr(0, 10)
var clave = entry.gd$when[0].endTime.substr(0, 10);
if(key in SelectedDates === false || clave in SelectedDates === false) {
SelectedDates[key] = [];
SelectedDates[clave] = [];
}
SelectedDates[key].push(entry.title.$t);
SelectedDates[clave].push(entry.title.$t);
});
$('#cal').datepicker("refresh");
});
});
$('#cal').datepicker(
{
beforeShowDay: function (date) {
var r = [true, ""];
if (SelectedDates === null) {
r[1] = "Highlighted-unknown";
}
else {
fechasPeriodo = [];
for(var property in SelectedDates) {
fechasPeriodo.push(new Date(property));
//alert(property);
}
var key = $.datepicker.formatDate("yy-mm-dd", date);
if(key in SelectedDates) {
if(fechasPeriodo.length > 1) {
r[1] = fechasPeriodo[0] <= date && date <= fechasPeriodo[1] ?"Highlighted"+SelectedDates[key][0].replace(/\s/g, "_"):"Highlighted-unknown";
}
else {
r[1] = "Highlighted"+SelectedDates[key][0].replace(/\s/g, "_");
}
r[2] = SelectedDates[key].join(", ");
}
}
return r;
},
minDate : fechaDefecto,
maxDate : fechaFin,
numberOfMonths: [3,3]
});
});
I'm hoping someone points out a cause, even if it's a criticism, because it's getting late.
I must say I tried this:
r[1] = new Date(fechasPeriodo[0].getYear(), fechasPeriodo[0].getMonth(), fechasPeriodo[0].getDate()) <= date && date <= new Date(fechasPeriodo[1].getYear(),fechasPeriodo[1].getMonth(),fechasPeriodo[1].getDate()) ?"Highlighted"+SelectedDates[key][0].replace(/\s/g, "_"):"Highlighted-unknown";
But didn't work either. May there be a problem in the way I'm representing the dates when I call new Date in the line fechasPeriodo.push(new Date(property)); and the way I'm comparing them?
It's just the difference between the UTC and non-UTC representation.
new Date('2014-01-30').toString(); //Wed Jan 29 2014 19:00:00
new Date('2014-01-30').toUTCString(); //Thu, 30 Jan 2014 00:00:00
Try fechasPeriodo[0].toUTCString(); and I'm pretty sure it will return what you expect.
I am using this code to generate 5 dates for my 'calendar' from the current week:
var days = new Array(4);
GetDaysOfWeek(new Date());
function GetDaysOfWeek(date)
{
for (var i = 0; i < 5; i++)
{
var $dd = (date.getDate() - date.getDay() + 1 + i);
var $mm = date.getMonth()+1;
var $yyyy = date.getFullYear();
if($dd<10){$dd='0'+$dd}if($mm<10){$mm='0'+$mm}
days[i] = $dd+'/'+$mm+'/'+$yyyy;
}
}
However depending what date it is it can return 00/10/2013 or 32/10/2013 on some of my boxes. It seems the only one updating correct is the actual date I am on.
How would I update those 5 dates so I would get the correct dates instead of non exist dates.
Thanks in advance.
currentTimestamp = new Date().getTime()
currentDay = currentTimestamp-currentTimestamp%(24*60*60*1000)
for (var i = 1; i< 6; i++){
nextDay = new Date(currentDay + i*24*60*60*1000)
days[i] = nextDay.toString(); //specify the format yourself
}
Here's a somewhat shorter way of creating an array of the dates of 5 weekdays from a given date;
function weekdays(d){
// set date [d] (or today) to the sunday before
var d = (function(){
this.setDate(this.getDate()-(this.getDay()));
return this;
}).apply(d || new Date);
// create an Array with length 5 and map 5 dates starting
// from date [d] into it
return String(Array(5)).split(',')
.map(function(){
return new Date(this.setDate(this.getDate()+1));
}, d);
}
It returns the weekdays from the first monday before the date, or, if the given date is a sunday, the dates of the weekdays from the next week.
weekdays();
//=> [Mon Oct 21 2013 17:14:23 GMT+0200, ... ,Fri Oct 25 2013 17:14:23 GMT+0200]
weekdays(new Date('2013/10/06'));
//=> [Mon Oct 07 2013 00:00:00 GMT+0200, ..., Fri Oct 11 2013 00:00:00 GMT+0200]
See also: Mozilla DN on map
Maybe this jsfiddle is of use to you?
I need to find out if two dates the user selects are the same in Javascript. The dates are passed to this function in a String ("xx/xx/xxxx").That is all the granularity I need.
Here is my code:
var valid = true;
var d1 = new Date($('#datein').val());
var d2 = new Date($('#dateout').val());
alert(d1+"\n"+d2);
if(d1 > d2) {
alert("Your check out date must be after your check in date.");
valid = false;
} else if(d1 == d2) {
alert("You cannot check out on the same day you check in.");
valid = false;
}
The javascript alert after converting the dates to objects looks like this:
Tue Jan 25 2011 00:00:00 GMT-0800 (Pacific Standard Time)
Tue Jan 25 2011 00:00:00 GMT-0800 (Pacific Standard Time)
The test to determine if date 1 is greater than date 2 works. But using the == or === operators do not change valid to false.
Use the getTime() method. It will check the numeric value of the date and it will work for both the greater than/less than checks as well as the equals checks.
EDIT:
if (d1.getTime() === d2.getTime())
If you don't want to call getTime() just try this:
(a >= b && a <= b)
var d1 = new Date($('#datein').val());
var d2 = new Date($('#dateout').val());
use two simple ways to check equality
if( d1.toString() === d2.toString())
if( +d1 === +d2)
var date = Wed Oct 07 2015 19:48:08 GMT+0200 (Central European Daylight Time);
var dateOne = new Date(date);
var dateTwo = new Date();
var isEqual = dateOne.getDate() === dateTwo.getDate()
this will give you the dates equality