Jump to content

Help with C# zellers calendar

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Guest_adamscott536_*

Guest_adamscott536_*
  • Guests
Hi all, just wondering if anyone can help me with a program i wish to make.
Basically it uses the standard Zellers algorithm to tell you what day a certain date was, but i also need it to say the day so it outputs e.g. "21st, August, 2011".
It also needs to regulate whether it was a leap year etc. Am having a bit of a problem with this and was wondering if anyone would like to help
This is the code i have written so far!
Thank you

using System;
using System.Collections.Generic;
using System.Text;

namespace Zeller
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("day");
int day = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("month");
int month = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("year");
int year = Convert.ToInt32(Console.ReadLine());
int m1, y0;
if (month < 3)
{
m1 = month + 10;
y0 = year - 1;
}
else
{
m1 = month - 2;
y0 = year;
}
int c1 = y0 / 100;
int y1 = y0 % 100;
int week = (day + ((26 * m1 - 2) / 10) + y1 + (y1 / 4) + (5 * c1) + (c1 / 4)) % 7;
string weekday = "";
switch (week)
{
case 0: weekday = "Sun"; break;
case 1: weekday = "Mon"; break;
case 2: weekday = "Tues"; break;
case 3: weekday = "Wednes"; break;
case 4: weekday = "Thurs"; break;
case 5: weekday = "Fri"; break;
case 6: weekday = "Satur"; break;
}



Console.WriteLine("{0} {1} {2} is a {3} day", day, mounth, year, weekday);
Console.ReadLine();
}
}
}

#2
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
Its almost a shame to tell you that its all built in... =)



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Globalization;


namespace ConsoleApplication1 {

    class Program {

        static void Main(string[] args) {

            Calendar calendar = CultureInfo.CurrentCulture.Calendar;

            DateTime dt = DateTime.Now.AddDays(73);

            Console.WriteLine("73 days from now will fall on this day of the week: {0}", calendar.GetDayOfWeek(dt));


            DateTimeFormatInfo dtfi = CultureInfo.CurrentCulture.DateTimeFormat;

            Console.WriteLine("73 days from now can be expressed in different ways:");


            Console.WriteLine("\t{0}", dt.ToString(dtfi.ShortDatePattern, CultureInfo.CurrentCulture));

            Console.WriteLine("\t{0}", dt.ToString(dtfi.LongDatePattern, CultureInfo.CurrentCulture));

            Console.WriteLine("\t{0}", dt.ToString(dtfi.RFC1123Pattern, CultureInfo.CurrentCulture));



            Console.WriteLine("Press <Enter> to terminate...");

            Console.ReadLine();

        }

    }

}







1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users