Software development, photography, jokes, ....

noyb-logo

Sites by me

 
sds82-logo
tas-logoTransportation Administration System
snoezelkussen-logo-kleinstSnoezelen Pillows for Dementia
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

Hosting Favorites

 
ANU Internet Services
Netim
GoDaddy
XEL Media

Blogroll

 
Bomenstichting
Google Translate
PHP
MySQL
jQuery
jQuery UI
YourHead Stacks API
Favicon Generator.
Password Generator.
Check HTTPS problems


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. I copied the code from RPG III to Javascript so I have that functionality elsewhere, too.



Buy me a tea


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');


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_stringToDate(datum) {
   /*
      IN:
         DD[-./]MM[-,/]YYYY
      OUT:
         YYYYMMDD
   */
   orgdate = datum;
   newdate = datum;

   if(orgdate.length == 7) {
      orgdate = '0' + orgdate;
   }
   else if(orgdate.length == 9) {
      orgdate = '0' + orgdate;
   }
   newdate = orgdate.split("-").reverse().join("");
   if(newdate == orgdate) {
      newdate = orgdate.split(".").reverse().join("");
      if(newdate == orgdate) {
         newdate = orgdate.split("/").reverse().join("");
         if(newdate == orgdate) {
            newdate = orgdate.substr(4,4) + orgdate.substr(2,2) + orgdate.substr(0,2)
         }
      }
   }

   return(newdate);
}

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
}



buy me something-2
© 1997- Marc Vos (and others)   -   Privacy Statement   -    Contact Me

On this website, Google Analytics is used to track visitor statistics. These are anonymised data about the number of visitors, which pages they visit on this site, from which regions they visit, which web browsers they use, etc.. You will also see non-personalised ads via Google AdSense. Cookies from Paddle or Paypal are placed when you click on a 'Buy now!' or 'Donate!' button, and possible cookies from Disqus when you use that system to comment on one or more blogposts.
Privacy Statement