I am building a paycheck month calculator. If you normally get paid every other week, most months will have two paychecks but two months of each year will have three paychecks.
My program works as expected, it appends to a list the date of everyday starting today (today being a Friday). Now I would like to make list items with the same month that appears five times stand out, simple coloring would work.
Can this be done with a second function for li in ul and then if li[i] string[4][6] === the next li[i][4][6] and then somehow search for five? I'm not sure how the logic would work. The reason why I picked [4][6] is because in each list item, the months are all three letter abbreviations that occupy the same part of the string.
How can this be done?
var d;
var week = 7;
function getDates() {
for (var i = 0; i < 52; i++) {
d = new Date();
d.setDate(d.getDate() + week);
$('#date').append("<li>" + d + "</li>");
week += 7;
};
}
$(document).ready(function() {
getDates();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="date"></ul>
A dirty example:
var d;
var week = 7;
function getDates() {
for (var i = 0; i < 52; i++) {
d = new Date();
d.setDate(d.getDate() + week);
$('#date').append("<li data-mth='"+d.getMonth()+"'>" + d + "</li>");
week += 7;
}
$('li[data-mth]').each(function(){
var $group = $('li[data-mth="'+ $(this).data("mth") +'"]');
if( $group.length > 4 ) $group.addClass("standOut");
});
}
$(getDates);
.standOut{
color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="date"></ul>
You could put dates in a array where the month is the key, then loop the months and if the count on that month == 5 colour all the dates.
var d;
var week = 7;
$(document).ready(function() {
getDates();
});
function getDates() {
var months = new Array;
for (var i = 0; i < 52; i++) {
d = new Date();
d.setDate(d.getDate() + week);
var month = d.getMonth();
if(!months[month]) months[month] = new Array;
months[month].push(d);
week += 7;
};
plot_months(months);
}
function plot_months(months){
$.each(months,function(key,month){
var style = "";
if(month.length > 4) style = "color:red;";
$.each(month,function(week,date){
$('#date').append('<li style="'+style+'">' +date+ "</li>");
});
});
}`
See this fiddle
Related
I'm wondering how to create single loop for create variable between today and start date.
var startDate = 11-15; /*that means 11th month of 2015*/
var d = new Date();
var m = d.getMonth() + 1;
var y = d.getFullYear().toString().substr(-2);
for (var i = 11; i <= m; i++) {
console.log(i);
}
Result should be 11-15, 12-15, 1-16 and so on until today 9-17. I don't know how I can add year into my code.
Assuming startdate is a string you can use below code
var startDate = "11-15"
var d = new Date();
var m = d.getMonth() + 1;
var y = d.getFullYear().toString().substr(-2);
d=startDate.split("-")
counter = parseInt(d[0])
for(var i=parseInt(d[1]);i<=parseInt(y);i++)
{
for(var j=counter;j<=12;j++){
if(j>m && i==y){
continue
}
console.log(j+"-"+i)
}
counter = 1;
}
Well you can use .split() over startDate which should be a string, and extract month and year and keep incrementing them respectively until you reach 09-17:
var startDate = "11-15";
var month = parseInt(startDate.split("-")[0]);
var year = parseInt(startDate.split("-")[1]);
var results = [];
while(!(year === 17 && month === 9)){
if(month<12){
month++;
}else{
month = 1;
year++;
}
console.log(month+'-'+year);
results.push(month+'-'+year);
}
console.log(results);
I would like to create a function that goes through the months of a given year, calculates how many Fridays fall on the 13th, and returns that number. So far this is what I have:
function numberOfFridaythe13thsIn(jahr){
var d = new Date();
d.setFullYear(jahr, 0, 13);
var counter = 0;
var months = 0;
while(months <= 11) {
months++;
if(d.getDay() == 5 && d.getDate() == 13) {
counter++;
}
}
return counter;
}
I am imagining this code starts on January 13th of a given year, has a counter where the sum of days will go, and will cycle through the months. I know my code is off, but can I get some guidance?
Try this:
function numberOfFridaythe13thsIn(jahr){
var d = new Date();
var counter = 0;
var month;
for(month=0;month<12;month++)
{
d.setFullYear(jahr, month,13);
if (d.getDay() == 5)
{
counter++;
}
}
return counter;
}
Basically, there are only twelve days in the year with date 13. So, we simply loop through each one and check if it is a Friday or not.
The important bit you were missing was to update the date on every loop iteration.
function numberOfFridaythe13thsIn(jahr) {
var count = 0;
for (var month=0; month<12; month++) {
var d = new Date(jahr,month,13);
if(d.getDay() == 5){
count++;
}
}
return count;
}
console.log(numberOfFridaythe13thsIn(2015));
I think it will help you...
function numberOfFridaythe13thsIn(jahr) {
var counter = 0;
for (i = 1; i <= 12; i++) {
var d = new Date(i + "/13/" + jahr);
if (d.getDay() == 5) {
counter++;
}
}
return counter;
}
it is for MM/dd/yyyy format.
I have some pure javascript calendar.
my problem is that I want to add the days before the choosen month.
|so|mo|di|mi|do|fr|sa|
______________________
|29|30|31|1 |2 |3 | 4| <-- here 29,30,31
______________________
|5 |6 |.....
I hope it's clear what I mean. Here my script
<script type="text/javascript">
function Calendar(id, year, month) {
var elem = document.getElementById(id)
var mon = month - 1 // (1)
var d = new Date(year, mon)
var table = ['<table><tr>']
for (var i=0; i<d.getDay(); i++) {
table.push('<td></td>') // here days before this mounth
}
// main body (3)
while(d.getMonth() == mon) {
table.push('<td>'+d.getDate()+'</td>')
if (d.getDay() % 7 == 6) { // (4)
table.push('</tr><tr>')
}
d.setDate(d.getDate()+1)
}
n = 1
for (var i=d.getDay(); i<8; i++) {
table.push('<td>' + (n++) + '</td>')
}
table.push('</tr></table>')
elem.innerHTML = table.join('\n')
}
new Calendar("cal", 2015, 9)
</script>
I try around with setDate() but I don't mastered it jet.
As mentioned in the comments setDate (and the Date constructor) can have negative values. But instead of a pre and post loop, you could set the startdate to the beginning of the week and always add entire weeks:
function Calendar(id, year, month) {
var elem = document.getElementById(id)
var mon = month - 1; // (1)
var d = new Date(year, mon, 1 );
var start = 1-d.getDay();
var table = ['<table>'];
while(d.getMonth() <= mon) {
table.push('<tr>');
for(var i =0 ; i< 7; i++){
d = new Date(year,mon,start++);
table.push('<td>'+d.getDate() + '</td>')
}
table.push('</tr>');
}
table.push('</table>')
elem.innerHTML = table.join('\n')
}
new Calendar("cal", 2015, 9);
fiddle
edit: an alternate version just in case the 'other' months should have a lighter colour: fiddle
Try defining start and end dates for your month view and iterate from start to end.
var startDate = new Date(d);
var endDate = new Date(d.getFullYear(), d.getMonth() + 1, d.getDate() - 1);
while (startDate.getDay() != 0) {
startDate.setDate(startDate.getDate() - 1);
}
while (endDate.getDay() != 6) {
endDate.setDate(endDate.getDate() + 1);
}
I have managed it in this way:
tmp = new Date(year, mon, 0).getDate() // return day of the last month
for (var i=0; i<(d.getDay() + 6) % 7; i++) { // count days
table.splice(1,0,'<td>'+(tmp-i)+'</td>') // adding days at first pos of table
}
Thanks
Date object has method .setDate() for adding days to get updated date.
var someDate = new Date(2015, 06, 05); // (year, month, date)
console.log('old date : ' + someDate);
var numberOfDays = 6; // No of day add or remove. use negative sign for remove days
someDate.setDate(someDate.getDate() + numberOfDays); //date object with new date (new day, new month and new year)
console.log('new date : ' + someDate);
I need to have an array of dates for whole days of the last week, including the current day, for e.g
['05/06', '04/06', '03/06', '02/06', '01/06', '31/05', '30/05']
(format dd/mm)
how can i do this?
I know there is the Date() object, but other than that I'm stumped.
logic along the lines of:
var dates = [];
var today = new Date();
for (var i = 0; i<7; i++){
var date = today - (i+1);
dates.push(date);
}
So you want an array containing todays date and a further 6 elements, with todays date-1, todays date-2 etc...?
var dates = [];
var date = new Date();
for (var i = 0; i < 7; i++){
var tempDate = new Date();
tempDate.setDate(date.getDate()-i);
var str = tempDate.getDate() + "/" + tempDate.getMonth();
dates.push(str);
}
console.log(dates);
Output: ["5/5", "4/5", "3/5", "2/5", "1/5", "31/4", "30/4"]
If you need numbers with leading 0's, try this:
var dates = [];
var date = new Date();
for (var i = 0; i < 7; i++){
var tempDate = new Date();
tempDate.setDate(date.getDate()-i);
var str = pad(tempDate.getDate()) + "/" + pad(tempDate.getMonth());
dates.push(str);
}
console.log(dates);
function pad(n) {
return (n < 10) ? ("0" + n) : n;
}
Output: ["05/05", "04/05", "03/05", "02/05", "01/05", "31/04", "30/04"]
Check this working sample, where all days are printed out:
http://jsfiddle.net/danyu/Tu5R6/6/
This is the main logic:
for(var i=7;i>0;i--)
{
tempDate.setDate(tempDate.getDate()-1);
output+=tempDate+"<br/>";
}
Modify it to store those days into your array.
I want to get the days of more then 1 month if pass 2 , 3 ,4 as number of month to the function, I want to get total number of days from today
function get_days_in_month(months){
days_in_month = new Date();
var y = days_in_month.getFullYear();
var m = days_in_month.getMonth();
if(months==1){
return new Date(y,m+1,0).getDate();
}
if(months==2){
// need to write code or some other logic
}
}
Thanks for help in advance
Use a for loop:
var total = 0;
for (i = 1; i <= months; i++) {
total += new Date(y, m + i, 0).getDate();
}
return total;
JSFiddle demo.