Calculate Start Date & End Date from Week & Year




Dates Calculation

In the below code, I have performed the calculation using start and end date, to understand in the detail, I would suggest you execute this code on your machine.


static void Job41(Args _args)
{
int weekJan1st;
int dayJan1st;
date tempDate,startDate,endDate;
int yr, week;
#TimeConstants
;
yr=2015; /* here i am assigning values in year and week */
week=54;
weekJan1st = wkofyr(dateStartYr(str2Date(strFmt(“%1-01-01”, yr), 321)));
dayJan1st = dayofwk(dateStartYr(str2Date(strFmt(“%1-01-01”, yr), 321)));
tempDate = dateStartYr(str2Date(strFmt(“%1-01-01”, yr), 321));
switch(firstWeekOfYear())
{
case 0: // Starts on Jan 1
tempDate -= (dayOfWk(tempDate) – 1);
tempDate += (week – 1 ) * 7;
break;
case 1: // First full week
//
// If 1st Jan is a Monday wkofyr gives the correct result
// If 1st Jan is a Tuesday-Thursday wkofyr gives one too much,
// as this 4/5/6 day week is given its own week number.
//
if (dayJan1st != 1)
tempDate += (8 – dayJan1st);
tempDate += (week – 1) * 7;
break;
case 2: // First 4-day week
if (mthofyr(tempDate) == 1 && weekOfYear(tempDate) > 50)
tempDate += week * 7;
else
tempDate += (week – 1) * 7;
tempDate -= (dayofwk(tempDate) – 1);
break;
}
startDate=tempDate-7;
endDate=tempDate-1;
info(strFmt(“%1,%2”,startDate,endDate));
}

No comments:

Post a Comment

Virtual Fields Vs Computed Fields

  Virtual Field: A virtual field in D365FO is a field that doesn't have a direct representation in the database. It's a field that y...