954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Validation

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?

foxypj
Newbie Poster
24 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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..

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

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

srikanthkadem
Junior Poster in Training
68 posts since Mar 2008
Reputation Points: 11
Solved Threads: 11
 
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

foxypj
Newbie Poster
24 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

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...

srikanthkadem
Junior Poster in Training
68 posts since Mar 2008
Reputation Points: 11
Solved Threads: 11
 

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

foxypj
Newbie Poster
24 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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...

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Check out this:

string dateString;
DateTime dateValue;

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

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

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.

srikanthkadem
Junior Poster in Training
68 posts since Mar 2008
Reputation Points: 11
Solved Threads: 11
 
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.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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.

foxypj
Newbie Poster
24 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 
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.

foxypj
Newbie Poster
24 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

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.

foxypj
Newbie Poster
24 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

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

foxypj
Newbie Poster
24 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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.

ChaseVoid
Junior Poster
116 posts since Aug 2007
Reputation Points: 40
Solved Threads: 12
 

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

foxypj
Newbie Poster
24 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You