473,320 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Better solution for getting Monday?

I need to get a Monday of the current week based on any date. I got the
function below working fine, but it feels hacky. And I can't find
anything in the framework. Is there a more elegant solution?

private DateTime GetMondayOfWeek(DateTime dt)
{
CultureInfo myCI = new CultureInfo("en-US");
while (myCI.Calendar.GetDayOfWeek(dt) != DayOfWeek.Monday)
dt = dt.AddDays(-1);

return dt;
}
Feb 2 '08 #1
1 4691
Try this:

private DateTime GetMondayOfWeek(DateTime dt)
{
CultureInfo myCI = new CultureInfo("en-US");

// Get the day of the week from the input.
DayOfWeek dow = myCI.Calendar.GetDayOfWeek(dt);
// Get the difference between that day and Monday
// (it might be negative if Monday is not the
// first day of the week)
int diff = dow - DayOfWeek.Monday;
// Correct if it's negative.
if (diff < 0)
diff += 7;
// Get the preceding Monday.
DateTime dtMonday = dt.AddDays(-diff).Date;

return dtMonday;
}

Tom Dacon
Dacon Software Consulting

"Frank Rizzo" <no**@none.netwrote in message
news:eg**************@TK2MSFTNGP06.phx.gbl...
>I need to get a Monday of the current week based on any date. I got the
function below working fine, but it feels hacky. And I can't find anything
in the framework. Is there a more elegant solution?

private DateTime GetMondayOfWeek(DateTime dt)
{
CultureInfo myCI = new CultureInfo("en-US");
while (myCI.Calendar.GetDayOfWeek(dt) != DayOfWeek.Monday)
dt = dt.AddDays(-1);

return dt;
}

Feb 2 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tiernan | last post by:
Hey everybody. I'm verry new to PHP and MYSQL and have been working on a form that when it is submitted stores the information into a mysql database. The main problem is that i'm trying to finish...
5
by: Eric Linders | last post by:
Hello, Today is Thursday, August 18, 2004. I would like to have a variable that stores the date this coming Sunday (even if today happened to be Sunday) in YYY-MM-DD format. I also need a...
5
by: Martien van Wanrooij | last post by:
I would like to retrieve, let us say, the First Monday after a certain date, so my (imaginary) function could be something like echo weekdayAfter("28 July 2005", "Monday") should return "1 August...
43
by: Rob R. Ainscough | last post by:
I realize I'm learning web development and there is a STEEP learning curve, but so far I've had to learn: HTML XML JavaScript ASP.NET using VB.NET ..NET Framework ADO.NET SSL
12
by: ce | last post by:
Hi, My company is using python currently for our website. We need to develop a GUI front-end for our ERP that would be portable (Windows and Linux). My question is which solution would be...
7
by: Rolf Mander | last post by:
What performs better, theoreticaly? This <? echo date("d",time()); echo date("m",time()); echo date("Y",time()); ?> Or that? <? $time = time();
4
by: sweetpotatop | last post by:
Hi, I wonder if there is any function in VB or SQL that will return the date of first Monday of the Monday. Besides, is there are a way finding the numbers of Monday in a month? Thanks in...
5
by: Chris | last post by:
I am trying to output Monday of the current week i.e. if Monday is the 8th I want to display 'Monday 8th' for any date between Monday 8-14th. I would appreciate any help, the code below is...
4
by: Vince | last post by:
Given a week Number, how do I calculate the date that for the Monday of that week?
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.