Search code examples
dayofweek

Get the first day of week of a year (any programming language)


How can I get the first day of the week of any given year (day being 1 to 7, or weekday name)?

I tried to figure it out in JavaScript, but I accept any other language.

I need to select a year to later build the full calendar (I thought using HTML tables and JavaScript), and for that I need to know at least the first day of the selected year.

I haven't found a solution or a question specifically dealing with finding the first day of any given year such that you only need to pass 1995, 2007, 1891. So if it's a repeated question please point the solution.

Do you have at least an online chart or DHTML site where I can see any full calendar for any year visually in that way?


Solution

  • In Javascript you can use this:

    getWeekDay = function (year) {
      var d = new Date(); 
      d.setFullYear(year,0,1);
      return d.getDay()+1;
    };
    
    document.write(getWeekDay(2011));
    

    Result is 1..7, as requested.