Get specific date (Day of Week)

  • Thread starter Thread starter Katit
  • Start date Start date
K

Katit

I need to get Date for Monday and Friday of current week.

I can't seem to find any better way then to DateTime.Now and loop
forward/backward incrementing/decrementing by 1 day until I reach
DayOfWeek.

Any better way to do that?
 
I need to get Date for Monday and Friday of current week.

I can't seem to find any better way then to DateTime.Now and loop
forward/backward incrementing/decrementing by 1 day until I reach
DayOfWeek.

Any better way to do that?

I would try using the DayOfWeek property to add or subtract
respectively to get to the date you need. Something like:

Today is Thursday to get to friday try DateTime.Now.AddDays(7 -
(int)DateTime.Now.DayOfWeek)

I didn't test this, just a thought
 
Katit,

If you know the value for the day of the week for the day you are
working with, then you can subtract that value from the DayOfWeek
enumeration from the DateTime. This will give you the Sunday for that week.
Then, you can just add Monday (1) or Friday (5) to get the DateTime
instances for those days of the week.
 
Katit,

If you know the value for the day of the week for the day you are
working with, then you can subtract that value from the DayOfWeek
enumeration from the DateTime. This will give you the Sunday for that week.
Then, you can just add Monday (1) or Friday (5) to get the DateTime
instances for those days of the week.

Yeah! Thats right..

Thanks!
 
There is one subtle bug here. You should store the value of Now into a
variable, as you are making the call to Now twice. On the off chance that
this is being run at midnight, it is possible that the first call to Now and
the second call to Now will produce DateTime values with different dates.
 
try this

DateTime dtNow=dateTime.now
int nowdayofweek = (int)dtNow.DayOfWeek;
DateTime weekStartDate, weekEndDate ;
weekStartDate = Microsoft.VisualBasic.DateAndTime.DateAdd("d", 0 - dtNow.Day, dtNow);
FridayDate = weekEndDate = Microsoft.VisualBasic.DateAndTime.DateAdd("d", 4 - dtNow.Day, dtNow);




Katit wrote:

Get specific date (Day of Week)
12-Jul-07

I need to get Date for Monday and Friday of current week

I can't seem to find any better way then to DateTime.Now and loo
forward/backward incrementing/decrementing by 1 day until I reac
DayOfWeek

Any better way to do that?

Previous Posts In This Thread:

Get specific date (Day of Week)
I need to get Date for Monday and Friday of current week

I can't seem to find any better way then to DateTime.Now and loo
forward/backward incrementing/decrementing by 1 day until I reac
DayOfWeek

Any better way to do that?

Re: Get specific date (Day of Week)

I would try using the DayOfWeek property to add or subtrac
respectively to get to the date you need. Something like

Today is Thursday to get to friday try DateTime.Now.AddDays(7
(int)DateTime.Now.DayOfWeek

I didn't test this, just a thought

Katit, If you know the value for the day of the week for the day you are
Katit

If you know the value for the day of the week for the day you are
working with, then you can subtract that value from the DayOfWeek
enumeration from the DateTime. This will give you the Sunday for that week.
Then, you can just add Monday (1) or Friday (5) to get the DateTime
instances for those days of the week

--
- Nicholas Paldino [.NET/C# MVP
- (e-mail address removed)


Re: Get specific date (Day of Week)
Yeah! Thats right.

Thanks!

There is one subtle bug here.
There is one subtle bug here. You should store the value of Now into a
variable, as you are making the call to Now twice. On the off chance that
this is being run at midnight, it is possible that the first call to Now and
the second call to Now will produce DateTime values with different dates

--
- Nicholas Paldino [.NET/C# MVP
- (e-mail address removed)



Submitted via EggHeadCafe - Software Developer Portal of Choice
Adding WCF Service References
http://www.eggheadcafe.com/tutorial...9-dfa51a9fab8e/adding-wcf-service-refere.aspx
 
try this

DateTime dtNow=dateTime.now
int nowdayofweek = (int)dtNow.DayOfWeek;
DateTime weekStartDate, weekEndDate ;
weekStartDate = Microsoft.VisualBasic.DateAndTime.DateAdd("d", 0 - dtNow.Day, dtNow);
FridayDate = weekEndDate = Microsoft.VisualBasic.DateAndTime.DateAdd("d", 4 - dtNow.Day, dtNow);




Katit wrote:

Get specific date (Day of Week)
12-Jul-07

That is basically what was suggested in the thread. Your code has a few
bugs and one ghastly coding style, using the VisualBasic namespece!

Your code should have been:

int nowdayofweek = (int)dtNow.DayOfWeek;
DateTime weekStartDate, weekEndDate ;
// use DayOfWeek, not Day (of month).
weekStartDate = Microsoft.VisualBasic.DateAndTime.DateAdd
("d", 0 - dtNow.DayOfWeek, dtNow);
DateTime FridayDate = weekEndDate =
Microsoft.VisualBasic.DateAndTime.DateAdd
("d", 5, weekStartDate);

But to do this without the vb namespace:

int nowdayofweek = (int)dtNow.DayOfWeek;
DateTime weekStartDate, weekEndDate;
Calendar c = new GregorianCalendar();
weekStartDate = c.AddDays(dtNow, (int) dtNow.DayOfWeek - 1);
DateTime FridayDate = weekEndDate = c.AddDays(weekStartDate, 5);
 
Hello Family,
That is basically what was suggested in the thread. Your code has a
few bugs and one ghastly coding style, using the VisualBasic
namespece!

Your code should have been:

int nowdayofweek = (int)dtNow.DayOfWeek;
DateTime weekStartDate, weekEndDate ;
// use DayOfWeek, not Day (of month).
weekStartDate = Microsoft.VisualBasic.DateAndTime.DateAdd
("d", 0 - dtNow.DayOfWeek, dtNow);
DateTime FridayDate = weekEndDate =
Microsoft.VisualBasic.DateAndTime.DateAdd
("d", 5, weekStartDate);
But to do this without the vb namespace:

int nowdayofweek = (int)dtNow.DayOfWeek;
DateTime weekStartDate, weekEndDate;
Calendar c = new GregorianCalendar();
weekStartDate = c.AddDays(dtNow, (int) dtNow.DayOfWeek - 1);
DateTime FridayDate = weekEndDate = c.AddDays(weekStartDate, 5);

Plus, the question was asked 2 1/2 years ago, so I doubt the OP is still
looking for an answer ;)

Karl
http://unlockpowershell.wordpress.com
 
I realize that this is an old thread, but none of the answers given were correct. I'm adding this in case anyone else stumbles across this entry:

var cal = new GregorianCalendar();
var today = DateTime.Now;
var monday = cal.AddDays(today, -((int)today.DayOfWeek)+1);
var friday = cal.AddDays(monday, 5);

You can make a couple of extension methods to do this quite easily:

public static class DateExtMethods
{
public static DateTime ThisWeekMonday(this DateTime dt)
{
var today = DateTime.Now;
return new GregorianCalendar().AddDays(today, -((int)today.DayOfWeek) + 1);
}

public static DateTime ThisWeekFriday(this DateTime dt)
{
var today = DateTime.Now;
return new GregorianCalendar().AddDays(today, -((int)today.DayOfWeek) + 6);
}
}


You can then do this:

var monday = DateTime.Now.ThisWeekMonday();
var friday = DateTime.Now.ThisWeekFriday();

Hope this helps someone else.
I need to get Date for Monday and Friday of current week.

I can't seem to find any better way then to DateTime.Now and loop
forward/backward incrementing/decrementing by 1 day until I reach
DayOfWeek.

Any better way to do that?
On Thursday, July 12, 2007 3:30 PM Jimmy V wrote:

I would try using the DayOfWeek property to add or subtract
respectively to get to the date you need. Something like:

Today is Thursday to get to friday try DateTime.Now.AddDays(7 -
(int)DateTime.Now.DayOfWeek)

I didn't test this, just a thought
On Thursday, July 12, 2007 3:35 PM Nicholas Paldino [.NET/C# MVP] wrote:
Katit,

If you know the value for the day of the week for the day you are
working with, then you can subtract that value from the DayOfWeek
enumeration from the DateTime. This will give you the Sunday for that week.
Then, you can just add Monday (1) or Friday (5) to get the DateTime
instances for those days of the week.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


news:[email protected]...
On Thursday, July 12, 2007 3:41 PM Nicholas Paldino [.NET/C# MVP] wrote:
There is one subtle bug here. You should store the value of Now into a
variable, as you are making the call to Now twice. On the off chance that
this is being run at midnight, it is possible that the first call to Now and
the second call to Now will produce DateTime values with different dates.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

news:[email protected]...
 
Back
Top