Calculate first and last date of a week

  • Thread starter Thread starter Hardy Wang
  • Start date Start date
H

Hardy Wang

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
 
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;
 
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?
 
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
(e-mail address removed)
 
Mark Johnson said:
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.
 
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
(e-mail address removed)
 
Back
Top