I am currently working on a project where i need check whether a date that is inputted is valid.

i know how to use this using repetitive if functions.

for example;

to check the month you do

if (month <1 || month >12)

Then to check the day which is my problem. Each month has a different amount of days in it. i can see how to do an if loop which then has an inner loop.

i.e

if (month == 1)
   {
     if (day <1 || day > 31)
         Application.Exit
    }

then i would go on and do that for all twelve months changing the if function where necessary. but is there a way to do this with arrays?

Recommended Answers

All 19 Replies

Have you considered that you are reinventing the wheel?
Perhaps there is a function of the datetime variety that will do all this for you..

Try with DateTme.TryParse method .let me know it is working for u.

Try with DateTme.TryParse method .let me know it is working for u.

ok that apears to be working. Thankyou

My final problem. How would i go about outputting the date that i have just validated. like this.

i put in 22/03/09 originally then the program works out whether the day needs a nd, st, rd or th at the end of it and then outputs like this;

22nd March, 2009

I was trying to get you to look at the datetime functions for yourself to find the things like tryparse..

Perhaps you will now try and do a search on date time formats.

i put in 22/03/09 originally then the program works out whether the day needs a nd, st, rd or th at the end of it and then outputs like this;

i did not get this words..can u explain indetails so that i can help u...

ok I have now got the date inputted by the user in this format 12/12/1990 and have used the tryparse method to check for validity,

I now need to output that date as 12th December, 1990

i.e

Date:

Console.WriteLine("please insert a date")
date = Convert.ToString(Console.ReadLine())

 String[] dateStrings = {date};
            DateTime dateValue;


            foreach (string dateString in dateStrings)
            {
                if (DateTime.TryParse(dateString, out dateValue))
                    goto output;
                else
                    Console.WriteLine("\r\n {0}", date);
                    Console.WriteLine("\r\nError)


                    goto Date
              }

I now want to output the date so that is outputs in a form like this 12th december 1990 instead of outputting as 12/12/09

Check out this:

string dateString;
DateTime dateValue;

Console.WriteLine("please insert a date");
dateString =Console.ReadLine();
if (DateTime.TryParse(dateString, out dateValue))
{
    //it is a date
}
else 
{
    //error
}

Flush goto's...

Check out this:

string dateString;
DateTime dateValue;

Why do you do this? This is not C. Predeclaration makes code less readable.

Hi Foxipj,

dateValue.ToString("dd MMMM,yyyy");
use this line in if condition .where datevalue is datetime object in ur code.

and u can play with this Tostring method as well for different formats.

Why do you do this? This is not C. Predeclaration

I know this is an old habbit I got from my Modula-2 times.
Thanks for reminding me.

That works well and outputs as

12 December, 1990

Which is great now all i need to do is make the program work out whether the day needs a th, nd, rd, st and make that output with the date.


i.e. the date should eventually output;

12th December 1990.

Take a read of the helpfile, there are many date/time formats, its very simple, but you should start reading for yourself what options you have.

Take a read of the helpfile, there are many date/time formats, its very simple, but you should start reading for yourself what options you have.

There is no default format to output in this way, hence why i am asking here.

So you didnt look.
and before you say you did..

I typed

DateTime.ToString("dd mm yyyy");
placed cursor on ToString
Pressed F1

Got help
Clicked on the datetime format string stuff

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

which has a second link to

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

You are just plain lazy

i have to chuckle at that. I did look at this site. Please go ahead and highlight the part that tells me how to add th, nd, st, rd on the end of the day?

yes i can add 3 april, 2009 which i could already do. Go ahead and tell me how to add the rest of it. There is no default coding in C# that has that effect and no coding in that help file.

After looking carefully through the internet carefully. it seems the only way to do it is to write and array that uses mathematics to work out which day needs which suffix. i am struggling with this writing such an array.

I have had some very good help from others on this site maybe they will help me.

It gets really tiring to see people who dont try and then expect others here to do the work. I know theres no date format that does the suffix, which is why I got you to look. If you google there are plenty of functions out there prewritten you can use to do it.

I too know theres no built in function to do it, it seems kind of a large oversight IMHO, but, there isnt. However, like most things like this, someone already has hit this and written something to do it. I got back a lot of hits with sensible code from google.

It gets really tiring to see people who dont try and then expect others here to do the work. I know theres no date format that does the suffix, which is why I got you to look. If you google there are plenty of functions out there prewritten you can use to do it.

I too know theres no built in function to do it, it seems kind of a large oversight IMHO, but, there isnt. However, like most things like this, someone already has hit this and written something to do it. I got back a lot of hits with sensible code from google.

Yes of course you can google it. I have done this already. and yes i agree that it is a large oversight.

i apologise for the outburst at you. Just rather frustrated with the coding, and finding it awfully difficult. please accept my apologese. i am sorry that you feel like i have not tried. i think that is an unfair assumption on me. however that is something i will not discuss as you have no right to judge me in that.

i will have another look at google and hopefully this time i can understand some of the codes that i get hit with. i am sorry that what seemed like a lovely site at first has ended up making me feel pretty down about my coding.

cheers then

Foxypj

You don't need to feel down. It's just that people tend to get irritated and fed up with those who just come around here looking for ready answers. I'm sure you must have tried, but you didn't write anything about it. If you are stuck somewhere ask for help on that part and people here will gladly correct you.

I hope you don't go away from the forum. Give us some of your trials and we'll help you out the best we can. Do you really think it's fair for us, who has the answer to give it those who nothing of it. it'll not only make us fell uncomfortable to help but also harms those who doesn't try out.

Hope you understand.

You don't need to feel down. It's just that people tend to get irritated and fed up with those who just come around here looking for ready answers. I'm sure you must have tried, but you didn't write anything about it. If you are stuck somewhere ask for help on that part and people here will gladly correct you.

I hope you don't go away from the forum. Give us some of your trials and we'll help you out the best we can. Do you really think it's fair for us, who has the answer to give it those who nothing of it. it'll not only make us fell uncomfortable to help but also harms those who doesn't try out.

Hope you understand.

Thank you to everyone who has helped me. I have finally solved this program. Liz my sincere apologies for my outbursts.

The help here has been awesome from everybody. Thank you.

foxypj

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.