Software development, photography, jokes, ....

Sites by me

 
tas-logoTransportation Administration System
snoezelkussen-logo-kleinstSnoezelkussens voor verstandelijk gehandicapten
ikzoekeenbegeleider-logoBegeleiders voor gehandicapten
Laat uw hond het jaarlijkse vuurwerk overwinnen
Betuweroute en Kunst
logo 50x50Hey Vos! Je eigen naam@vos.net emailadres?
Kunst in huis? Nicole Karrèr maakt echt bijzonder mooie dingen
nettylogo2Kunst in huis? Netty Franssen maakt ook bijzonder mooie dingen
Professionele opvang bij Gastouderbureau
Kind-Zijn
Salarisadministratie en belastingadvies bij
De Zaak Loont
Zutphense Bomenstichting

Hosting Favorites

 
ANU Internet Services
XelMedia .internet Services
register.com

Blogroll

 
Bomenstichting
LassoSoft
MacFreak
Quality that computes
The Economy of Motion
Wheel 2.0
IntrAktv



Website Hosting bij Xel Media

Marc's Place


 

 Day or Date of the Monday of a week

  Includes a working Javascript example at the bottom of this page
jslogo64


I needed a Javascript function to get the beginning and ending dates of a week of a given year. Since I could not find one on the net, I googled for shorthands and looked up some of my old 3GL code I wrote back in 1992 (still functioning on iSeries, though) and put the two together into a Javascript function to get the date of the monday of a week, in some format. Don’t ask me what I do in mv_monday where I calculate an offset, it’s been so long since I wrote this code - I simply copied it from RPG III to Javascript - that I really don’t know anymore how and why. But it still works, and that is what counts.

Here’s the code (also all together in mv_clcdat.js):

function mv_leapck(yy, dorf) {
   //   dorf = 1=Days or 0=Flag

   if(new Date(yy,1,29).getDate() == 29)
      return(1 + (dorf==1 ? 365 : 0));
   else
      return(0 + (dorf==1 ? 365 : 0));
}

function mv_isDateObject(d) {
   if (typeof d == 'object') {
      if(d.getTime === undefined)
         return(0);
      else;
         return(1);
   } else
      return(0);
}

function mv_dateToString(datum, format) {
   /*
      IN:
         YYYYMMDD
         YYYYDDD
         YYYYWW or YYYYMM
         or a date object
      OUT:
         Anything. Depends on format.
         %d = day (DD or DDD)
         %m = month
         %w = week
         %y = yy
         %Y = yyyy
   */
   var txt = '';
   var fmt = new String(format);

   if(mv_isDateObject(datum))
      txt = new String(datum.getFullYear() * 10000 + (datum.getMonth() + 1) * 100 + datum.getDate());
   else
      txt = new String(datum);

   if(txt.length >= 6) {
      fmt = fmt.replace('%y', txt.substr(2,2));
      fmt = fmt.replace('%Y', txt.substr(0,4));

      if(txt.length == 8) {
         fmt = fmt.replace('%m', txt.substr(4,2));
         fmt = fmt.replace('%d', txt.substr(6,2));
      }
      else if(txt.length == 7)
         fmt = fmt.replace('%d', txt.substr(4,3));
      else if(txt.length == 6)
         fmt = fmt.replace('%w', txt.substr(4,2));
   }

   return(fmt);
}

function mv_mondayOfWeek(yy, ww, mdd) {
   /*
      MONDAY returns the day number or the date
      of the monday in the week ww of year yy.

      IN:
      - 4-digit year
      - weeknumber
      - mdd = 2=Milliseconds, 1=Day or 0=Date
         
      OUT:
      - Day = YYYYDDD (DDD = Day number in year YYYY)
      - Date = YYYYMMDD
   */
   var daymsec = 1000*60*60*24;
   var wrk1 = wrk2 = 0;

   // Calculate offset
   wrk1 = yy - 1900 + parseInt((yy - 1900 - 1) / 4);
   wrk1 -= parseInt(wrk1 / 7) * 7;
   if(wrk1 > 3) wrk1 -= 7;
   wrk2 = (ww * 7) - wrk1 - 6;

   if (wrk1 < 0 && ww == 53) {
      yy--;
      wrk2 -= 371;
      wrk2 += mv_leapck(yy, 1);
   }
   else if (wrk2 <= 0) {
      yy--;
      wrk2 += mv_leapck(yy, 1);
   }

   // Format return value
   if(isNaN(parseInt(mdd)) || parseInt(mdd) == 0) {
      wrk1 = (new Date(yy-1, 11, 31)).getTime();
      wrk1 += wrk2 * daymsec; // in milliseconds
      wrk2 = new Date();
      wrk2.setTime(wrk1);   // Date of monday in week ww
      wrk1 = wrk2.getFullYear() * 10000 + (wrk2.getMonth() + 1) * 100 + wrk2.getDate();
   } else if(parseInt(mdd) == 2) {
      wrk1 = (new Date(yy-1, 11, 31)).getTime();
      wrk1 += wrk2 * daymsec; // in milliseconds
   } else {
      wrk1 = (yy * 1000) + wrk2;
   }
            
   return(wrk1);   // Date or Day of monday
}


Input year (YYYY): and week
 
Monday of the week is:
Day number: mv_dateToString(mv_mondayOfWeek(selYY, selWW, 1), '%d-%Y')
Date: mv_dateToString(mv_mondayOfWeek(selYY, selWW, 0), '%d-%m-%Y')
Last day of week: var weekmsec = 1000*60*60*24*6;
var d = new Date();
d.setTime(mv_mondayOfWeek(selYY, selWW, 2) + weekmsec); // add 6 days
mv_dateToString(d, '%d-%m-%Y');

© 1997- Marc Vos (and others) Contact Me