Tag Archives: month

Computer Algorithms: How to Determine the Day of the Week

Introduction

Do you know what day of the week was the day you were born? Monday or maybe Saturday? Well, perhaps you know that. Everybody know the day he’s born on, but do you know what day was the 31st January 1883? No? Well, there must be some method to determine any day in any century.

We know that 2012 started at Sunday. After we know that it’s easy to determine what day is the 2nd of January. It should be Monday. But things get a little more complex if we try to guess some date distant from January the 1st. Indeed 1st of Jan was on Sunday, but what day is 9th of May the same year. This is far more difficult to say. Of course we can go with a brute force approach and count from 1/Jan till 9/May, but that is quite slow and error prone.

Following Days
If 1st of January is Sunday the most logical thing to happen is 2nd of January to be Monday

So what we’ll do if we have to code a program that answers this question. The most easier way is to use a library. Almost every major library has built-in functions that can answer what day is on a given date. Such are date() in PHP or getDate() in JavaScript. But the question remains. How these library functions know the answer and how can we code such library function if our library doesn’t support such functionality?

There must be some algorithm to help us. Continue reading Computer Algorithms: How to Determine the Day of the Week

javascript get locale month with full name

JavaScript Date object

The Date object in JS helps you work with dates as it sounds logical. However there are limited number of functions attached to it which you can use. You can check for full Date specification in w3schools : here.

How to get the month name?

Normally when you try to use a date object you can format it with the following code:

var d = new Date();
alert(d.getFullYear());
alert(d.getMonth());
alert(d.getDate());

The problem is that you cannot reference the date object and to get the full name of the month. Something like d.getFullMonth()!

That’s why the easiest way to get the full month name is to make a custom locale array such this one:

var monthLocale = new Array(12);
monthLocale[0] = "January";
monthLocale[1] = "February";
monthLocale[2] = "March";
monthLocale[3] = "April";
monthLocale[4] = "May";
monthLocale[5] = "June";
monthLocale[6] = "July";
monthLocale[7] = "August";
monthLocale[8] = "September";
monthLocale[9] = "October";
monthLocale[10] = "November";
monthLocale[11] = "December";

And than you can simply reference the locale array!