469,939 Members | 2,362 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,939 developers. It's quick & easy.

Calculate first and last date of a week

Hi:
Are there any algorithms I can use, that based on a given date and a
culture code (different cultures may have different beginning of week), I
can get first and last date of the current week?

Thanks!

--

WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
Nov 15 '05 #1
5 40169
using System.Globalization;
using System.Threading;

CultureInfo info = Thread.CurrentThread.CurrentCulture;
DayOfWeek firstday = info.DateTimeFormat.FirstDayOfWeek;
DayOfWeek today = info.Calendar.GetDayOfWeek(DateTime.Now);

int diff = today - firstday;
DateTime firstDate = DateTime.Now.AddDays(-diff);
MessageBox.Show(firstDate.ToShortDateString());

Sadly, this does not work. DayOfWeek does not follow international
standards and will put Sunday as the first day independent of culture.

Try using (this assumes if not sunday, it has to be monday)
int diff = 0;
if(firstday != DayOfWeek.Sunday)
if(today == DayOfWeek.Sunday)
diff = 6;
else
diff = today - firstday;

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #2

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opryevibn6hntkfz@localhost...
Sadly, this does not work. DayOfWeek does not follow international
standards and will put Sunday as the first day independent of culture.


So where is Sunday not the first day of the week? Four thousand years of
tradition aren't good enough?
Nov 15 '05 #3
In European contries Monday is the first day of the week.
God worked 6 days and rested on the last day of the week (Sunday).
At least accourding to the (Christian) Bible.

In Israel Sunday is the first (working) day since Sabbat is from Friday
Sundown to Saterday Sundown.
The same for the Islamic world.

Just as most of the World sort it's Dates (DD.MM.YYY or YYYY.MM.TT) only 2
Cultures support an unsorted Date-Format (MM.TT.YYY) - they are US-English
and Swaili.

The DateTimePicker for NET.Framework (PC) supports the first day of weeks
for culture but not the samle DateTimePicker.cs that the offered for the
NET.Framework.Compact. I had work out these conditions to get it to work for
all supported languages.

BTW it is new to me that the US is 4000 years old

Mark Johnson, Berlin Germany
mj*****@mj10777.de

"Michael A. Covington" <lo**@www.covingtoninnovations.com.for.address>
schrieb im Newsbeitrag news:eJ*************@TK2MSFTNGP10.phx.gbl...

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opryevibn6hntkfz@localhost...
Sadly, this does not work. DayOfWeek does not follow international
standards and will put Sunday as the first day independent of culture.


So where is Sunday not the first day of the week? Four thousand years of
tradition aren't good enough?

Nov 15 '05 #4
Mark Johnson <mj*****@mj10777.de> wrote:
In European contries Monday is the first day of the week.
God worked 6 days and rested on the last day of the week (Sunday).
At least accourding to the (Christian) Bible.


No, in Christian circles Sunday is the first day of the week. God
rested on the Sabbath, Saturday. I can't remember exactly when the
"rest day" moved from Saturday to Sunday for Christians, but Sunday is
most definitely the start of a Christian week in the UK at least.

Monday is the start of a business week just because that's more
convenient, I suspect.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
This is no doubt the reason why the Jews/Arabs "rest" on Saturday.
Be it as it may why the Christian "rest" on Sunday and not on Sunday - the
official
first day of the week here is Monday (as shown on the Calendars) so it must
be supported
when supporting Internationalsation.
I built the DateTimePicker to support all days of the week as the first day
so everyone should be happy.
Of cource if Mars has 8 or 10 days the week I am sunk - but i can live with
that at the moment

Mark Johnson, Berlin Germany
mj*****@mj10777.de

"Jon Skeet [C# MVP]" <sk***@pobox.com> schrieb im Newsbeitrag
news:MP************************@msnews.microsoft.c om...
Mark Johnson <mj*****@mj10777.de> wrote:
In European contries Monday is the first day of the week.
God worked 6 days and rested on the last day of the week (Sunday).
At least accourding to the (Christian) Bible.


No, in Christian circles Sunday is the first day of the week. God
rested on the Sabbath, Saturday. I can't remember exactly when the
"rest day" moved from Saturday to Sunday for Christians, but Sunday is
most definitely the start of a Christian week in the UK at least.

Monday is the start of a business week just because that's more
convenient, I suspect.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Targa | last post: by
1 post views Thread by simon | last post: by
4 posts views Thread by Morten Wennevik | last post: by
1 post views Thread by Martin Emanuelsson | last post: by
reply views Thread by Lee Harr | last post: by
9 posts views Thread by ice | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.