-5

how can I write program in c# that show the number of the day

for example :

I ask a user to type a day and he write Monday and I want to show him 2

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Maasoud Asadi
  • 61
  • 4
  • 8

2 Answers2

1

Try to use DateTime.Now.DayOfWeek. Definition:

namespace System
{
    using System.Runtime.InteropServices;

    [Serializable, ComVisible(true), __DynamicallyInvokable]
    public enum DayOfWeek
    {
        [__DynamicallyInvokable]
        Friday = 5,
        [__DynamicallyInvokable]
        Monday = 1,
        [__DynamicallyInvokable]
        Saturday = 6,
        [__DynamicallyInvokable]
        Sunday = 0,
        [__DynamicallyInvokable]
        Thursday = 4,
        [__DynamicallyInvokable]
        Tuesday = 2,
        [__DynamicallyInvokable]
        Wednesday = 3
    }
}

Example:

// For your case Monday == 2
int todayDayOfWeekNumber = (int)DateTime.Now.DayOfWeek + 1;
dyatchenko
  • 2,283
  • 3
  • 22
  • 32
0

Try this:

(System.DateTime.Now.Day) % 7;     //will return integer 2 for Monday

Example:

DateTime inputDate = System.DateTime.Now;
System.Windows.Forms.MessageBox.Show(((inputDate.Day)%7).ToString());

OR:

Using DayOfWeek (just like dyatchenko mentioned):

((int)DateTime.Now.DayOfWeek + 1)

DayOfWeek starts from Sunday (with 0). So if you want to get 2 for Monday, just add 1.

Raging Bull
  • 18,593
  • 13
  • 50
  • 55
  • `value ranges from zero (which indicates DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday)` https://msdn.microsoft.com/fr-fr/library/system.datetime.dayofweek%28v=vs.110%29.aspx – Xaruth Feb 23 '15 at 11:20