AngularJS Tutorial Series: Part 1 – Building a Calendar with LESS CSS, Font Awesome, and Moment

Published Nov 26, 2014Last updated Aug 14, 2017

This intermediate AngularJS mini tutorial is part 1 of a 5 part series with each teaching you how to build something you can use on your web page. This tutorial will teach you how to make a calendar using AngularJS directives among other tools.

Other Posts in the Series:

Today, I’m going to talk about how to create a calendar control using Angular JS, LESS CSS, Font Awesome and Moment. I’m going to assume you’re at least halfway familiar with these tools, but if you’re not, I suggest you take a look using the links above.

The calendar itself will be an Angular JS directive that allows the user to select a date, which is set on a controller’s property. I’ve styled the calendar, and I’ll include that style in this tutorial, but you can obviously feel free to tweak how it looks to your heart’s content. If you’re the kind of reader that prefers to view a completed product and just check the source yourself, I’ve put together a demo page which shows off the calendar. You can see it here.

I’m going to break down the write up into a couple of different sections. First, the HTML. It’s pretty straightforward, as it’s just the calendar directive HTML. Then, we’ll walk through the JavaScript code, which shows how I’m using Angular JS to update the directive’s state which, in turn, updates the calendar view. Finally, I’ll describe what I’m doing with each of the CSS rules that style the calendar. While most of the CSS is simple, there are few things I’m going to go over that might be new to the reader.

Note: The CSS shown here might not match up with exactly what you see on the demo page because I want the demo to look at least halfway decent with some pretty styling. For the most part, though, I’m going to try to keep the CSS on the tutorial as bare as possible.


Directive Usage

<calendar selected="day"></calendar>

In this case, the variable on the controller that gets updated with the selected date would be “day”.

Calendar Template

Because the calendar template is quite large, I’ve extracted it and placed it in its own HTML file, which is the best practice.

<div class="header">
    <i class="fa fa-angle-left" ng-click="previous()"></i>
    <span>{{month.format("MMMM, YYYY")}}</span>
    <i class="fa fa-angle-right" ng-click="next()"></i>
<div class="week names">
    <span class="day">Sun</span>
    <span class="day">Mon</span>
    <span class="day">Tue</span>
    <span class="day">Wed</span>
    <span class="day">Thu</span>
    <span class="day">Fri</span>
    <span class="day">Sat</span>
<div class="week" ng-repeat="week in weeks">
    <span class="day" ng-class="{ today: day.isToday, 'different-month': !day.isCurrentMonth, selected: }" ng-click="select(day)" ng-repeat="day in week.days">{{day.number}}</span>

Most of this is straightforward, but there are a couple of things I should go over. First, we’re using Font Awesome to render the arrows that allow the user to change months. Font Awesome uses a series of CSS classes to perform the rendering, which in our case are “fa-angle-left” and “fa-angle-right”, with a base “fa” class, as well. You don’t have to use the I tag, but it’s how the Font Awesome guys show all their examples, so I tend to follow along.

The next line shows how we’re using Angular JS and Moment to render the currently selected day’s month. The month variable is where we’re storing the day’s month value. We’re using Moment’s format function to render just the month name. Note that the month is a separate variable from the selected date because the user can select a date outside of the selected month, using the trailing and leading weeks. For example, the end of a month might be on a Wednesday, but we pad out the rest of the week using days from the next month.

The last HTML section contains the rendering for each of the weeks and days. We’re looping through the list of weeks (as shown using Angular’s ng-repeat directive), and then another loop inside that to render the days of the week (another ng-repeat). The syntax for the ng-repeat directive is a little strange, but straightforward. “week in weeks” indicates that we want to iterate over weeks, and assign each item in the weeks array to the week variable. It’s similar to the array.forEach method. Then, we’re setting a couple of styles for each day depending on the state of our directive. For example, we want the selected day and today to have a different style from regular days. We accomplish this using Angular’s ng-class directive, which sets a CSS class on the element based on a truthy variable. Finally, we’re setting up an ng-click directive which allows us to select a day.


Here’s the Angular code for setting up the calendar directive, as well as the controller for our test page. To reduce confusion, I would break out the controller and directive into separate files, as is Angular best practice. For brevity’s sake, I’ve included all the script in one section.

var app = angular.module("demo", []);

app.controller("calendarDemo", function($scope) {
    $ = moment();

app.directive("calendar", function() {
    return {
        restrict: "E",
        templateUrl: "templates/calendar.html",
        scope: {
            selected: "="
        link: function(scope) {
            scope.selected = _removeTime(scope.selected || moment());
            scope.month = scope.selected.clone();

            var start = scope.selected.clone();

            _buildMonth(scope, start, scope.month);

   = function(day) {
                scope.selected =;  

   = function() {
                var next = scope.month.clone();
                _buildMonth(scope, next, scope.month);

            scope.previous = function() {
                var previous = scope.month.clone();
                _buildMonth(scope, previous, scope.month);
    function _removeTime(date) {

    function _buildMonth(scope, start, month) {
        scope.weeks = [];
        var done = false, date = start.clone(), monthIndex = date.month(), count = 0;
        while (!done) {
            scope.weeks.push({ days: _buildWeek(date.clone(), month) });
            date.add(1, "w");
            done = count++ > 2 && monthIndex !== date.month();
            monthIndex = date.month();

    function _buildWeek(date, month) {
        var days = [];
        for (var i = 0; i < 7; i++) {
                name: date.format("dd").substring(0, 1),
                isCurrentMonth: date.month() === month.month(),
                isToday: date.isSame(new Date(), "day"),
                date: date
            date = date.clone();
            date.add(1, "d");
        return days;

First, we’re setting up the Angular application for the demo. Then, we set up the controller for the page, and finally, we create the calendar directive. The only thing we’re doing in the controller is setting the day variable the calendar sends its selected day to. The calendar directive itself is a little more complex. First, we set a starting value for the selected day, based on whether or not the controller’s day is set yet. If it isn’t, we just use today’s date.

We use Moment to set the time to midnight, and then generate the current start date for the calendar’s initial month. Then we create the month by calling the _buildMonth function, which sets a list of weeks on the scope. That, in turn, calls the _buildWeek function, which does the same thing for days. The day object contains utility properties which indicate various things, like whether the current day comes before the selected month, or if the day is today. Finally, we add some methods to the scope that allow the user to change months and select a day. The select function is trivial; it just sets the scope’s selected variable to the passed in day. The previous and next methods decrement and increment the month, respectively, and then rebuild the current month.


.vertical-centre (@height) {
    line-height:@height !important;

.border-box {

calendar {
    border:solid 1px @border-colour;

    >div.header {
        >* { 
        >i {
            padding:0 @spacing;
        >i.fa-angle-left {
        >i.fa-angle-right {
        >span { 
    >div.week {
        border-top:solid 1px @border-colour;
        &:first-child {
        > {
            border-left:solid 1px @border-colour;
            &:first-child {
            &.today {
            &.different-month {
            &.selected {
        &.names>span {

The first few lines define a few helpful mixins using LESS CSS. The first allows us to centre elements vertically, presuming we have the height of the wrapping element. The second sets the box-sizing property, which Firefox doesn’t support without the -moz- prefix.

Next, we’re setting some variables related to styling and spacing that are used more than once. If you want to change the colour, for example, the secondary-colour variable is what you need to update. The rest is just standard styling to make the calendar look somewhat pretty and spacing stuff to make sure it’s aligned properly.


And that’s it! As I mentioned above, I’ve put together a demo page which shows off the calendar in its final form. You can see it here.

Thanks for reading!

Discover and read more posts from Chris Harrington
get started