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