2

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?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
alt.126
  • 1,097
  • 1
  • 9
  • 22

5 Answers5

4

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.

phlogratos
  • 13,234
  • 1
  • 32
  • 37
  • 2
    The result will be 0-6, unless you add 1, as that's what JavaScript returns from `Date.getDay` [MDN Date Documentation](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getDay) – twip Jul 26 '11 at 19:24
  • Just pointing out that it returns 0 to 6. It's just a matter of adding 1 to the result to get 1 to 7. – alt.126 Jul 26 '11 at 21:20
  • In modern JS: `new Date('2023-01-01').toLocaleDateString('en', { weekday: 'long' }); // => Sunday` – Przemyslaw Mar 30 '23 at 21:13
2

With .NET's BCL:

return new DateTime(year, 1, 1).DayOfWeek; // DayOfWeek enum value

In Noda Time:

return new LocalDate(year, 1, 1).IsoDayOfWeek; // IsoDayOfWeek enum value

In Java using the built-in classes:

Calendar calendar = Calendar.getInstance();
calendar.set(year, 1, 1);
return calendar.get(Calendar.DAY_OF_WEEK); // 1 (Sunday) - 7 (Saturday)

In Java using Joda Time:

return new LocalDate(year, 1, 1).getDayOfWeek(); // 1 (Monday) - 7 (Sunday)
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

Pretty easy with C# and .NET:

using System;

public class SomeClass
{
    public static void Main()
    {
        DateTime dt = new DateTime(2000,1,1);
        Console.WriteLine(dt.DayOfWeek);

        dt = new DateTime(2010,1,1);
        Console.WriteLine(dt.DayOfWeek);
    }
}

Output:

Saturday 
Friday

The Java version:

import java.util.Date;
import java.util.GregorianCalendar;

public class test3
{
    public static void main (String[] args)
    {
        GregorianCalendar c = new GregorianCalendar(2000,1,1);  
        Date d = c.getTime();
        System.out.println(d.getDay());

        c = new GregorianCalendar(2010,1,1);    
        d = c.getTime();
        System.out.println(d.getDay());
    }
}

Output:

2
1
Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • 1
    Date.getDay is deprecated, and I believe the results will depend on your time zone. Not a good idea. `c.get(Calendar.DAY_OF_WEEK)` is more appropriate. – Jon Skeet Jul 26 '11 at 19:25
2

At this Wikipedia article, look for Sakamoto's algorithm.

alx
  • 19
  • 3
1

This site http://5dspace-time.org/Calendar/Algorithm.html has a good explanation on how to calculate it even with pencil-and-paper

Wikipedia explains it too

woliveirajr
  • 9,433
  • 1
  • 39
  • 49