Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 25th, 2007, 1:42 PM   #1
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 712
Rep Power: 10 OpenLoop is on a distinguished road
Day of the week

Hey guys,
I was writing a date conversion program the other day and I couldn't find a C# library function to figure out the day of the week on a specific day. I ended up writing this simple function and thought to share it:
        private string dayOfWeek(int year, int month, int day)
        {
            int[] monthStartDay = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
            string[] dayNames = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };            
            int daysSince1900 = (monthStartDay[month - 1] + day + (year - 1900) * 365 + (year - 1900 - 1) / 4 - 1);
            int dayIndex = daysSince1900 % 7;
            return dayNames[dayIndex];
        }
OpenLoop is offline   Reply With Quote
Old Aug 25th, 2007, 2:28 PM   #2
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 10 Dameon is on a distinguished road
CultureInfo.CurrentCulture.Calendar.GetDayOfWeek(DateTime.Now)

Requires System.Globalization namespace.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Aug 25th, 2007, 2:31 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 8,368
Rep Power: 19 DaWei will become famous soon enoughDaWei will become famous soon enough
Man, how cool is that? We used to have to use Zeller's Congruence.
__________________
Contributor's Corner:
Politically Incorrect
DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 26th, 2007, 11:21 AM   #4
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 712
Rep Power: 10 OpenLoop is on a distinguished road
CultureInfo... I would never suspect it. You know, I googled for an hour and didn't come across it, or Zeller's Congruence. I guess I'll just post here next time. Thanks.
OpenLoop is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Little late, but in honor of Engineering Week Mjordan2nd Coder's Corner Lounge 9 Mar 1st, 2007 5:42 AM
caluculate day of week given present date and number of days srinivasc_it C++ 4 Nov 4th, 2006 4:54 AM
How Unlucky has my week been prodigy27c Coder's Corner Lounge 38 Jul 25th, 2005 10:25 AM
Getting Start Date of Week Automatik C# 2 Jul 20th, 2005 4:20 PM
Gone for a week Mjordan2nd Coder's Corner Lounge 14 Mar 15th, 2005 12:08 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:53 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC