Hi,

I am trying to make a program which formats the date for the user and verifies leapyears and checks for invalid dates.

Below i have worked out the code to calculate leap years. But how would i go about formating the date so for example:

If the user entered the date 25/12/2010 the output would be "25th December 2010".

I guess i need an array to store the months and the days in a month and the "st, nd, rd, th" suffix's to the days.?

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

namespace Date_Calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            int day, month, year;
            Boolean leapYear;

            Console.WriteLine("Enter the day");
            day = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the month");
            month = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the year:");
            year = Convert.ToInt32(Console.ReadLine());

            if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
                leapYear = true;
            else
                leapYear = false;

            Console.WriteLine("The date {2} {3} {0} is a leap year: {1}.", year, leapYear, day, month);
            Console.ReadLine();
        }   
    }
}

Any help appreciated.

i dont think u should save the prefixes in the array.
there are cultures for writing the date are available.
you can apply this code

//For ex., Convert MM/dd/YYYY to dd/MM/yyyy
string date = "03/27/2008"; //format is MM/dd/yyyy
DateTimeFormatInfo dateTimeFormatterProvider = DateTimeFormatInfo.CurrentInfo.Clone() as DateTimeFormatInfo;
dateTimeFormatterProvider.ShortDatePattern = "MM/dd/yyyy"; //source date format
DateTime dateTime = DateTime.Parse(date, dateTimeFormatterProvider);
string formatted = dateTime.ToString("dd/MM/yyyy"); 
//write the format in which you want the date tobe converted
Response.Write("<br />" + formatted);

and for validation you can use javascript function.

and your Leap year can be done like this

if(DateTime.IsLeapYear(year))
leapYear = true;
else
leapYear = false;

Thanks for the help.

Just a quick question. Is it possible to calculate the validity of a date without using C# built in methods?

Ok, I have fixed the validity problem and write my own methods.

Do you guys know how i would go about converting the date from:

d/M/yyyy into dd, MMMM yyyy

Thanks again.

If you have your date in a DateTime, use the ToString method with any format specifier you want. See the link I gave you.

The Date is in three different int variables in the form of int day, int month and int year. Is there a way to convert them to DateTime and convert them to the dd MMMM yyyy?

Try this:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int day = 18;
            int month = 1;
            int year = 1952;
            DateTime birthday = new DateTime(year, month, day);
            Console.WriteLine("My birthday is: {0}", birthday.ToString("dd MMMM yyyy"));
            Console.ReadLine();
        }
    }
}

yes it is possible to validate a date string without using c# validations.use the javascript on your .aspx page you can easily find the date validation function in javascript on internet

Thanks for your help guys it all works perfectly now!:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.