Madcowe
Madcowe

Reputation: 243

C#, how do I use a dateTimePicker's value on if statements?

The work I'm doing consists on finding out one's astral sign, so I put a dateTimePicker to select the date of birth, but now I'm not sure how I will get the data from that dateTimePicker to check which sign is it...

Like for example, from january first to february 14th you are this sign, from this to that you're... you know what I mean.

Upvotes: 0

Views: 4200

Answers (5)

Alex R.
Alex R.

Reputation: 4754

You are not concerned about the year. You just need the month and day values:

DateTimePicker dtp = new DateTimePicker();

int month = dtp.Value.Month;
int day = dtp.Value.Day;

Then, you can do if-else statements around the various astral signs:

if (month >= 3 && day >= 20 && month < 4 && day <21 )
{
    return AstralSign.Aries;
}
else if (/* some other sign */)
{
    // ...
}

You can create an enum for the signs as well:

enum AstralSign
{
   ...
   Aries
   ...
}

Upvotes: 2

Oliver
Oliver

Reputation: 45119

The DateTimePicker has a property Value. This contains the selected date by the user.

If you like to get informed when the user made his selection, simply subscribe to the ValueChanged property.

Afterwards simply compare if the the given date falls within the range of any astral sign.

To get the astral sign i would built up a nested dictionary Dictionary<int, Dictionary<int, AstralSign>> which could then be access by

selectedSign = astralSigns[pickedDate.Month][pickedDate.Day];

Upvotes: 1

V4Vendetta
V4Vendetta

Reputation: 38230

There is nothing in built in for Zodiac signs , so i would recommend you building your own list for the Zodiac signs.

Comparing a date falls in which zodia part you can go along with DayOfYear part of the Datetime value selected in the DatePicker (dateTimePicker1.Value)

So 0 to 46 could be your first zodiac and compare this with the DayOfYear part and you will have your Zodiac sign (else year would be a problem while comparing DateTime objects)

Upvotes: 1

Greg
Greg

Reputation: 1723

If your DateTimePicker was called dateTimePicker, you could do it like this:

DateTime pickedDate = new DateTime(dateTimePicker.Value.Year, dateTimePicker.Value.Month, dateTimePicker.Value.Day);

And then you could use pickedDate as you want.

Upvotes: 1

Haris Hasan
Haris Hasan

Reputation: 30107

There is no built-in functionality in DateTimePicker for achieving this. You will have to either go for if-else approach or a switch statement to find out which sign a particular date belongs to..

Upvotes: 1

Related Questions