JS getDate comparison doesn't work on certain dates - calendar

I'm working on a simple calendar on my website, and I'm trying to accomplish the following:
Today's date should be on a dark background color (css class: nyt)
Future days should appear on a lighter background color (css class: tuleva)
Past days should appear with 0.5 opacity (css class: menny)
This is the javascript code that I'm using:
$(function() {
var date = new Date(),
currentDate = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" +
date.getDate();
$(".kalenteripv").each(function() {
var specifiedDate = $(this).data('date');
if (specifiedDate == currentDate) {
$(this).addClass('nyt');} // this day
else if (currentDate > specifiedDate) {
$(this).addClass('menny');} // past days
else {$(this).addClass('tuleva');} // future days
});
});
Today (2017-12-2), for some reason the calendar is showing the dates 10-19 on 0.5 opacity, so the code is adding the "menny" or past dates' css class to those dates, which it obviously shouldn't do. I tried already to write the dates with two digits (e.g. 2017-12-02), but when I do that, the whole code stops working.
Here's an image to clarify the problem with dates 10-19
Any suggestions? It must be some kind of simple mistake that I'm making, but I really can't figure it out myself. I hope I explained the problem clear enough.
Here's the fiddle that shows the problem.
https://jsfiddle.net/vfy1bfnz/
Thank you in advance!

$(function pvmr() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd = '0'+dd
}
if(mm<10) {
mm = '0'+mm
}
today = yyyy + "-" + mm + "-" + dd;
$(".kalenteripv").each(function pvmr() {
var specifiedDate = $(this).data('date');
if (specifiedDate == today) {
$(this).addClass('nyt');
} else if (today < specifiedDate) {
$(this).addClass('tuleva');
} else if (today > specifiedDate) {
$(this).addClass('menny');
}
});
});
Solved it! I thought I should leave the answer here in case someone needs the code some day.

Related

I need to highlight a datepicker method only for a specific input field

There are 7 input fields in my code but I want to use them for only one. Also there are multiple pages where this datepicker is used all done through a common datepicker directive. I don't want the other pages and input fields to get affected with this change. Here is my code :
beforeShowDay: function(date) {
var month = date.getMonth() + 1;
var year = date.getFullYear();
var day = date.getDate();
if (day < 10) {
day = '0' + day;
}
if (month < 10) {
month = '0' + month;
}
var newDate = month + "/" + day + "/" + year;
if (jQuery.inArray(newDate, highlight_date) != -1) {
//console.log(newDate);
return [true, "highlight"];
}
return [true];
}

How to convert readable date time in timestamp which comes from backend

I want to convert the commit date and time to time stamp which I get from my APIs.
But I don't know how to do this in angular?
Here is my controller code :
var commitDate = item.commitMetaData.commitDate;
var dat = new Date(commitDate);
But it says "Invalid Date"
PS: Thanks in advance
What you could do is generate the date from the values montValue , year, dayOfMonth
with plain Javascript you could just do
var d = new Date();
d.setMonth(commitDate.monthValue +1); //see explanation below
d.setDate(commitDate.dayOfMonth);
d.setYear(commitDate.year);
be careful the months start at 0 so January is actually 0 so in your example you would have to add +1
You can also create a filter for this
.filter('createDate', function ($filter) {
return function (input) {
if (input != null && input != undefined) {
var d = new Date();
d.setMonth(input.monthValue +1); //see explanation below
d.setDate(input.dayOfMonth);
d.setYear(input.year);
return d;
}
};
})
and call it like
var commitDate = item.commitMetaData.commitDate;
var dat = $filter('createDate')(commitDate);
reference JS Date

Modifying a plugin, but changes aren't reflected. AngularJS in an Ionic (Angular2) app

I am trying to modify a plugin, it is a calendar. When you press on the dates that are not part of the month (at the beginning and the end of the month) it changes to the next or previous month. I am trying to disable this, and I found what part of the code to change, but when I make the changes (or add a console.log message), nothing gets reflected when I execute ionic serve. One thing is, the calendar is written in AngularJS - while I am using Ionic, which uses Angular 2. The part of the code I am trying to change looks like this:
$scope.select = function (viewDate) {
console.log("in in in select &&&&&&&");
var selectedDate = viewDate.date,
events = viewDate.events,
views = scope.views,
dates,
r;
if (views) {
dates = views[scope.currentViewIndex].dates;
var currentCalendarDate = ctrl.currentCalendarDate;
var currentMonth = currentCalendarDate.getMonth();
var currentYear = currentCalendarDate.getFullYear();
var selectedMonth = selectedDate.getMonth();
var selectedYear = selectedDate.getFullYear();
var direction = 0;
if (currentYear === selectedYear) {
if (currentMonth !== selectedMonth) {
direction = currentMonth < selectedMonth ? 1 : -1;
}
} else {
direction = currentYear < selectedYear ? 1 : -1;
}
ctrl.currentCalendarDate = selectedDate;
if (direction === 0) {
if (ngModelCtrl) {
ngModelCtrl.$setViewValue(selectedDate);
}
var currentViewStartDate = ctrl.range.startTime,
oneDay = 86400000,
selectedDayDifference = Math.floor((selectedDate.getTime() - currentViewStartDate.getTime()) / oneDay);
for (r = 0; r < 42; r += 1) {
dates[r].selected = false;
}
if (selectedDayDifference >= 0 && selectedDayDifference < 42) {
dates[selectedDayDifference].selected = true;
scope.selectedDate = dates[selectedDayDifference];
}
} else {
console.log("is getting here &&^^%%$$");
//ctrl.moveOnSelected = true;
//ctrl.slideView(direction); <----- I AM COMMENTING THIS OUT TO STOP THE SLIDE!!!
}
...
At the bottom of the above code block, I made an arrow pointing to the two lines I am commenting out. I also added console.log statements, one at the beginning of the function, and one in the middle, and neither of them are output into the console (the above is the select function which happens when a date on the calendar is selected).
The code structure of the plugin is like this:
angular.module("ui.rCalendar.tpls", ["templates/rcalendar/calendar.html","templates/rcalendar/day.html","templates/rcalendar/displayEvent.html","templates/rcalendar/month.html","templates/rcalendar/monthviewDisplayEvent.html","templates/rcalendar/monthviewEventDetail.html","templates/rcalendar/week.html"]);
angular.module('ui.rCalendar', ['ui.rCalendar.tpls'])
.constant('calendarConfig', {
formatDay: 'dd',
formatDayHeader: 'EEE',
formatDayTitle: 'MMMM dd, yyyy',
formatWeekTitle: 'MMMM yyyy, Week w',
formatMonthTitle: 'MMMM yyyy',
formatWeekViewDayHeader: 'EEE d',
formatHourColumn: 'ha',
calendarMode: 'month',
showEventDetail: true,
startingDayMonth: 0,
startingDayWeek: 0,
allDayLabel: 'all day',
noEventsLabel: 'No Events',
eventSource: null,
queryMode: 'local',
step: 60,
autoSelect: true,
monthviewDisplayEventTemplateUrl: 'templates/rcalendar/monthviewDisplayEvent.html',
monthviewEventDetailTemplateUrl: 'templates/rcalendar/monthviewEventDetail.html',
weekviewAllDayEventTemplateUrl: 'templates/rcalendar/displayEvent.html',
weekviewNormalEventTemplateUrl: 'templates/rcalendar/displayEvent.html',
dayviewAllDayEventTemplateUrl: 'templates/rcalendar/displayEvent.html',
dayviewNormalEventTemplateUrl: 'templates/rcalendar/displayEvent.html'
})
.controller('ui.rCalendar.CalendarController'
...
.directive('monthview', ['dateFilter', function (dateFilter) {
THIS IS WHERE THE SELECT FUNCTION IS
...
This is an angularjs plugin in an Ionic app, remember it is built on Angular 2 not angularjs. Im not sure where the plugin javascript file gets loaded.
I was looking in the wrong package folder, I didn't realized I had installed a typescript version.

Display all days(dates) between two dates in angularjs

I want to use select "From" and "To" dates from datepicker and display all dates in between, in tabular form using angularjs.
Try this function to calculate no. of days. Hope it helps
function daysCalculator(date1, date2) {
var d1 = new Date(date1);
var d2 = new Date(date2);
var days = d1.getTime() - d2.getTime();
days = parseInt((((days / 1000) / 60) / 60) / 24)
return days;
}
Try using this.
function getDates(fromDate, toDate) {
var dateArray = [];
var nextDate = fromDate;
while (nextDate <= toDate) {
dateArray.push( nextDate );
nextDate.setDate(fromDate.getDate() + 1);
}
return dateArray;
}

Angularjs - Time difference between my data time and CURRENT TIME

I am trying to display the time difference between my {{trade.timer}} and the current time but coudn't succeed after many tries.
I am looking to make the $scope.gains[i].timer = vtime - "CURRENTTIME"
Here is my code:
$scope.updatetimerValue = function (timerValue){
$.each(timerValue, function(k, v) {
for (var i =0; i < $scope.gains.length ; i ++) {
if($scope.gains[i].orderid == v.orderid){
$scope.gains[i].timer = v.time;
}
}
});
}
<td>{{gain.timer | date: 'HH:mm'}}</td>
Any idea?
Note: v.time time format is yyyy-MM-dd HH:mm:ss
You can get date difference between two days with basic javascript.
var date = new Date('10/27/2014');
var currentDate = new Date();
var milisecondsDiff = date-currentDate;
var secondsDiff = miliseconds/1000;
var minutesDiff = seconds/60;
var hoursDiff = minutes/60;
var daysDiff = hours/24;
Also I suggest don't mix up AngularJS and JQuery.
And instead $.each use the angular.forEach
angular.forEach(values, function(value, key) {
///
});
or even better to use simple for loop, because it works faster.

Resources