• Stars
    star
    177
  • Rank 215,985 (Top 5 %)
  • Language
    HTML
  • License
    MIT License
  • Created over 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple Calendar built with Pure JavaScript (No Libraries) http://iamnitinpatel.com/projects/calendar/

Calendar with Pure JavaScript and HTML

The program uses Date() function to build a simple calendar with Pure JavaScript and HTML. Here's the final implementation of it - http://iamnitinpatel.com/projects/calendar

alt html javascript calendar

Explanation- When the program is started, the function showCalendar() is with arguments currentMonth and currentYear. This function populates the table with dates at the correct places.

The buttons "previous", "next" and dropdown "jump" control the functions, previous(), next() and jump(). These function update the currentMonth and currentYear variable.

How showCalendar Works-

showCalendar(month, year) function which takes in two parameters, month and year. Once, the function is called, it dynamically generates a calendar in HTML and appends it into our table. Here’s my approach.

Get the starting day of the month, we’ll use -

let firstDay = (new Date(year, month)).getDay();

Next, get the number of days in that month. We can achieve this too using date function.

let daysInMonth = 32 - new Date(year, month, 32).getDate();

Explanation, the function new Date(year, month, 32) returns the 32nd day after the month started. If we subtract that date from 32, we get the final day of that month. Example, If we pass feb 2018 as an argument, its ‘32nd’ day will be 4th of march, subtract 32 from 4 and we get 28, final day of the month of feb 2018.

Once we have the two things ready, we populate the table with numbers 1 to [last day of month] on appropriate places. For example, if the starting of that month is Thursday and Ending date is 28, we’ll put the number 1 below thursday, 2 below, friday, 3 below saturday and so on. When we reach 28, we break out of the loop.

Here, we use a nested for loop because we have upto 6 rows and 7 columns.

In the outer loop, we create a new “tr” element, ie, table row, up to 6 times. (maximum number of rows we need to create the calendar), then run the inner loop to create elements in each rows, then append those rows into our table.

In the inner loop, we populate each row with “td” elements in it. We keep track of the date using variable “date” in it.There are three if conditions at each iteration:

If we’re at first row and we have not yet reached first yet, create td element and leave it blank.

If “date” is higher than max days in that month, we break out of the loop because we have finished creating the table.

Else, we create the “td” element and print the “date” in it.

More Repositories

1

calculator-react

A simple arithmetic Calculator built with ReactJS.
JavaScript
60
star
2

expense-tracker

Expense Tracker built with MERN Stack
CSS
30
star
3

happy-birthday-react-p5

Wish someone Happy Birthday with React and p5.js!
JavaScript
23
star
4

nodejs-image-upload

NodeJS/Express web app to get image from user and upload it to the server.
HTML
22
star
5

addVeryLargeNumbers

Algorithm for adding large numbers (>53 bits). Takes in any two numbers but in the form of strings, and returns the sum of both numbers as string.
JavaScript
7
star
6

linkedin-login-react

LinkedIn authentication button for React
JavaScript
5
star
7

extra-long-factorials

Factorials of large numbers (output is not limited to 53 bits which is default limit of javascript number storage)
JavaScript
4
star
8

haskell-snake

Snake game written in Haskell
Haskell
3
star
9

HTML-and-CSS

Basic HTML & CSS files, Google Clone.
HTML
3
star
10

weather-info-map-react

React application that displays a map and weather information of the place when clicked.
JavaScript
3
star
11

monty-hall-simulation

Simulation of famous monty hall game in JavaScript.
JavaScript
3
star
12

whatsapp-desktop-app

Using Electron.js to build a desktop application for Whatsapp.
JavaScript
2
star
13

Single-Layer-Perceptron

Using JavaScript to implement a the basic building block of Neural Networks.
JavaScript
2
star
14

tictactoe-minimax-haskell

Tic Tac Toe game with AI
Haskell
2
star
15

Parenthesis-Matching-with-Reduce-Method

Use of Reduce method in Javascript to to solve problem of parenthesis matching.
JavaScript
2
star
16

arweave-url-shortener

URL Shortening website that uses the Arweave Blockchain as backend.
JavaScript
1
star
17

battery-life

Display device's battery life with HTML & JS. (No libraries used)
JavaScript
1
star
18

rattle-battle-entry

JavaScript
1
star
19

js-python-quine

JS program that prints a C program that prints a Python program that prints back the original program.
JavaScript
1
star
20

htmlTableToJSON

Get Data from HTML table to JSON using DOM.
JavaScript
1
star