472,779 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

year/week calculation

hey all,
is there a way if you are given a Year and a Week an easy way to go back say
26 weeks ago from given year/week.

for example,
given: 2008/16
26 weeks prior is:
2007/43

thanks,
rodchar
Nov 21 '08 #1
3 5732
Since the length of a year is not a multiple of 7 days, the problem seems
ill-defined.

"rodchar" <ro*****@discussions.microsoft.comwrote in message
news:E7**********************************@microsof t.com...
hey all,
is there a way if you are given a Year and a Week an easy way to go back
say
26 weeks ago from given year/week.

for example,
given: 2008/16
26 weeks prior is:
2007/43

thanks,
rodchar
Nov 21 '08 #2
TAB
As Microsofts weeknumber has bug you can't use the built in
Calendar.GetWeekOfYear because that won't
return the correct ISO8601 week number.

I'm using this replacement. One way to use it is to subtract 1 day at a time
and count weeks until
you found the date you are looking for.
// get week number for current date
public int WeekNumber(DateTime fromDate)
{
// Get jan 1st of the year
DateTime startOfYear = fromDate.AddDays(-fromDate.Day +
1).AddMonths(-fromDate.Month + 1);
// Get dec 31st of the year
DateTime endOfYear = startOfYear.AddYears(1).AddDays(-1);
// ISO 8601 weeks start with Monday
// The first week of a year includes the first Thursday, i.e. at
least 4 days
// DayOfWeek returns 0 for sunday up to 6 for Saturday
int[] iso8601Correction = { 6, 7, 8, 9, 10, 4, 5 };
int nds = fromDate.Subtract(startOfYear).Days +
iso8601Correction[(int)startOfYear.DayOfWeek];
int wk = nds / 7;
switch (wk)
{
case 0:
// Return weeknumber of dec 31st of the previous year
return WeekNumber(startOfYear.AddDays(-1));
case 53:
// If dec 31st falls before thursday it is week 01 of
next year
if (endOfYear.DayOfWeek < DayOfWeek.Thursday)
return 1;
else
return wk;
default: return wk;
}
}

"rodchar" <ro*****@discussions.microsoft.comskrev i meddelandet
news:E7**********************************@microsof t.com...
hey all,
is there a way if you are given a Year and a Week an easy way to go back
say
26 weeks ago from given year/week.

for example,
given: 2008/16
26 weeks prior is:
2007/43

thanks,
rodchar
Nov 21 '08 #3
rodchar wrote:
hey all,
is there a way if you are given a Year and a Week an easy way to go back say
26 weeks ago from given year/week.

for example,
given: 2008/16
26 weeks prior is:
2007/43
With ISO week numbering I actually get 2007/42.

Below are some code. You will need to add week
numbering scheme if you are not using ISO - just
add value to enum WeekType and case to switch in
WeekUtil.FromDate.

Arne

============================

using System;
using System.Globalization;

namespace E
{
public enum WeekType { ISO };
public static class WeekUtil
{
private static int IsoWeek(int year, int mon, int day)
{
int a = (14 - mon) / 12;
int y = year + 4800 - a;
int m = mon + 12*a - 3;
int JD = day + (153 * m + 2)/5 + 365*y + y/4 - y/100 +
y/400 - 32045;
int d4 = (((JD + 31741 - JD % 7) % 146097) % 36524) % 1461;
int L = d4 / 1460;
int d1 = ((d4 - L) % 365) + L;
return d1 / 7 + 1;
}
public static YearWeek FromDate(WeekType typ, DateTime dt)
{
int y;
int w;
y = dt.Year;
switch (typ) {
case WeekType.ISO:
w = IsoWeek(dt.Year, dt.Month, dt.Day);
break;
default:
throw new ArgumentOutOfRangeException("Unknown week
type");
}
if(w == 1 && dt.Month == 12) y++;
if(w >= 52 && dt.Month == 1) y--;
return new YearWeek(typ, y, w);
}
public static DateTime ToDate(WeekType typ, YearWeek yw)
{
DateTime dt = new DateTime(yw.Year, 1, 7);
dt = dt.AddDays((yw.Week - FromDate(typ, dt).Week) * 7);
return dt;
}
}
public struct YearWeek
{
private WeekType typ;
private int y;
private int w;
public int Year { get { return y; } }
public int Week { get { return w;} }
public YearWeek(WeekType typ, int y, int w)
{
this.typ = typ;
this.y = y;
this.w = w;
}
public YearWeek AddWeeks(int n)
{
return WeekUtil.FromDate(typ, WeekUtil.ToDate(typ,
this).AddDays(n * 7));
}
public override string ToString()
{
return Year.ToString("0000") + "w" + Week.ToString("00");
}
public static bool operator==(YearWeek a, YearWeek b)
{
return a.Year == b.Year && a.Week == b.Week;
}
public static bool operator!=(YearWeek a, YearWeek b)
{
return !(a == b);
}
public override bool Equals(object obj)
{
return this == (YearWeek)obj;
}
public override int GetHashCode()
{
return y.GetHashCode() ^ w.GetHashCode();
}
}
public class Program
{
public static void Main(string[] args)
{
YearWeek yw = new YearWeek(WeekType.ISO, 2008, 16);
for (int i = 0; i <= 26; i++)
{
Console.WriteLine(yw + "->" + yw.AddWeeks(-i));
}
Console.ReadKey();
}
}
}
Nov 22 '08 #4

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

Similar topics

3
by: bad | last post by:
i´ve two variables the year and the week (2003 and 5) that means the 5th week of the year 2003. now i need the start- and enddate of the 5th week of year 2003. i hope someone can help me to...
21
by: .:mmac:. | last post by:
I have to update a page every week. I get the page ahead of time so I used the "scheduled includes" webbot in Frontpage only to find that I would have to refresh the page every week to have it...
1
by: MissiMaths | last post by:
This isn't really an access question as I can write the code myself(I hope) but need to know how the start of the financial year is worked out. If someone knows the rules or an algorithm, I would...
7
by: peter | last post by:
I have created a MS Access query where one of the fields displays the week number. The field is a date value that uses the ww format to display the week number. However, when I try to create a...
2
by: | last post by:
Hi!! know someone how to get a week span with week number and year? something like hi: public static DateTime GetWeekSpan(int weekNumber, int year) { DateTime weekStartDate = new...
5
by: Aljosa Mohorovic | last post by:
i use this to find out current week and total number of weeks for current year: now = datetime.now() weeks_in_year = int(date(now.year, 12, 31).strftime("%W")) current_week = int(date(now.year,...
14
by: Tommy Jakobsen | last post by:
Hi. Is there a method in .NET that takes "year" as an argument and returns the total number of weeks in that year? For culture da-DK (Danish). Thanks in advance. Tommy.
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.