Coder Social home page Coder Social logo

naylinnpkv / calendarhtml-javascript Goto Github PK

View Code? Open in Web Editor NEW

This project forked from niinpatel/calendarhtml-javascript

1.0 0.0 0.0 8 KB

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

Home Page: http://iamnitinpatel.com/projects/calendar/

License: MIT License

HTML 65.66% JavaScript 34.34%

calendarhtml-javascript's Introduction

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();
  1. 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.