Day of the Week

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi!

I'm beginer in C# so excuse me on silly questions but i hawe to start somehow.

I want to get a name of the day in the Week generated from a number.

For instance i have variable named; int Day = 2;
and then i have another variable named; string DayOfWeek;

and now i want to set it like this:

DayOfWeek = WeekDay(Day);

but i don't know witch funktion can i use to do this? Can someone help me?

Thanks!

G.
 
DayOfWeek day = (DayOfWeek)2; // Sun = 0, Mon = 1, Tue = 2
Console.WriteLine(System.Globalization.DateTimeFormatInfo.CurrentInfo.GetDayName(day));

There is also a DayNames array if you prefer.

Marc
 
I don't know if we understand eachother i need some funktion to convert me
integer value to string name of day. For instance

int dayName = 2;

string NameOfDay =
System.Globalization.DateTimeFormatInfo.CurrentInfo.GetDayName(dayName);

and i get
NameOfDay is Tuestay

That's all.



G.
 
Which is pretty much what I posted - isn't it?

Your code won't currently compile because the GetDayName function (which
does this job) doesn't accept an int - it accepts a DayOfWeek (enum); all I
have done is add tje required cast between the integer and the enum (adding
a comment to explain).

You could, however, do this

int dayName = 2;
string NameOfDay =
System.Globalization.DateTimeFormatInfo.CurrentInfo.GetDayName((DayOfWeek)
dayName);

Which is almost identical to the code I posted, except without the comment!

So where are we not agreeing?

If you don't need to worry about international issues, you could even cheat
and use:

string NameOfDay = ((DayOfWeek)dayName).ToString();

But I wouldn't recommend it...

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top