13,189,506 members (62,614 online)
Tip/Trick
alternative version

#### Stats

5.6K views
3 bookmarked
Posted 30 Sep 2011

# Date on Day Of the Week

, 2 Oct 2011
 Rate this:
Day of the week
I had come across a requirement wherein I had to send some reports to the user on the first day of the month and on the last day of the month based on the day selected by the Admin (e.g., the Day of the week provided by Admin is Sunday).

Well, here is the logic which does the trick:

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
namespace ConsoleApplication3
{
class Demo
{
static void Main(string[] args)
{

//This was a Interview Question
//where in i want to find the first
int i=(int)DayOfWeek.Saturday  ;
int z = (int)FindLastDate.DayOfWeek ;
DateTime LastDay = FindLastDate.AddDays(z >= i ? (-z + i) : -(z + 7 - i));
Console.WriteLine("The Date Of The Last " + DayOfWeek.Saturday.ToString() + " Of the Month Is" + LastDay.ToShortDateString());
DateTime FindFirstDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
z = (int)FindFirstDay.DayOfWeek;
DateTime FirstDay = FindFirstDay.AddDays(z > i ? (7 - z) + i : (7 - z)-(7-i));
Console.WriteLine("The Date Of The First " + DayOfWeek.Saturday.ToString() + " Of the Month Is" + FirstDay.ToShortDateString());
}
}
}```

## Share

 United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Social Group (No members)

## You may also be interested in...

 First Prev Next
 Reason for my vote of 5 good logic instead of going for forl... Santoshyy10-Oct-11 18:36 Santoshyy 10-Oct-11 18:36
 Reason for my vote of 5 good logic instead of going for forloop
 Last Visit: 31-Dec-99 18:00     Last Update: 16-Oct-17 16:32 Refresh 1