Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculation 5

Status
Not open for further replies.

tsp1lrk

IS-IT--Management
May 30, 2001
103
US
Hello,

I'm trying to figure out code for a calculation I'm working with- I need to determine a date and a year from the following information: (uses the 2nd number)

237= 2(2002); 37 - 13 = Week 24 of 2002 = June 2002

Another one:
212= 2(2002); 12 - 13 = Week 51 of 2001 = December 2001

Using a different number:
437 = 4(2004); 37 – 13 = Week 24 of 2004 = June 2004

I'm using ASP.net but I could also use a Javascript for this and I have 2 textboxes (Or I guess I could use one as well) to collect the info. I'm stuck trying to determine how to begin- Can anyone help me? Appreciate any insight on this one-

Thanks,
Lisa
 
tsp1lrk,

That was fun.
Try alot of your values to see if it works like you need.

Code:
<html><head><title>TEST</title>
<script language=&quot;JavaScript&quot;>
var months = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug',
  'Sep','Oct','Nov','Dec');
function cdwk(str){
wkmsec = 86400000 * 7;
wk = str.substr(1,2);
int_wk = parseInt(wk);
prn = str.indexOf(&quot;(&quot;)+ 1;
yrstr = str.substr(prn,4);
int_yr = parseInt(yrstr);
wkc = int_wk - 13;
yrc = int_yr;
	if(wkc < 1){
	wkc = 52 + wkc;
	yrc = int_yr - 1;
	}
ndstr = &quot;Jan 1, &quot;+yrc;
nds = new Date(ndstr);
ndb = (nds - 0)+ (wkc * wkmsec);
ndal = new Date(ndb);
wknum = ((ndal - 0)-(nds - 0))/wkmsec;
mnth = months[ndal.getMonth()];
yr = ndal.getYear();
alert(wknum+' week of '+yr+' :: '+mnth+' '+yr);
}
</script></head><body>
<input type=&quot;text&quot; size=&quot;20&quot; onBlur=&quot;cdwk(this.value)&quot; 
value=&quot;237= 2(2002)&quot;>
</body></html>



Great Javascript Resource:
 
Hey!! It almost works-- The user is going to put a 3 digit number(manufacturing code) in a textbox, then click calculate; I took out your value and put in a 3 digit number, like 437- This is what I got:

24 week of 437 :: June 437 - so it's almost working- something's not right-This would be awesome if we could get this working! Thanks so much ;>)

Lisa

 
The script is built so that it reads this:

237= 2(2002)

What formats do you intend to let the user enter?
We can make it work I just have to know what you are
allowing.

I won't be back in the office till late afternoon.
See you then.
 
Hi,

The form is just going to have one textbox for then to enter a 3 digit number- could be anything- but it will be 3 digits. The first digit will always indicate the year-for example:

437 - Will mean 2004
321 - Will mean 2003
212 - Will mean 2002 and so on-

I just need them to enter a 3 digit code, click calculate and then get what you provided-
XX Week of XX Year :: XXmonth XX year

Thanks!
Lisa
 
Had a bit of a play. :)

Does this do the trick:
Code:
<script Language=&quot;JavaScript&quot;>
<!--
function monthNo(z) {
	var months=new Array(&quot;January&quot;, &quot;February&quot;, &quot;March&quot;, &quot;April&quot;, &quot;May&quot;, &quot;June&quot;, &quot;July&quot;, &quot;August&quot;, &quot;September&quot;, &quot;October&quot;, &quot;November&quot;, &quot;December&quot;);

	// Rough guess about number of days in Feb - ignore Leap years. :)
	var mdays=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var x=0;	// Index for month
	var y=0;	// Total days accumulated

	var month=&quot;&quot;;
	for(x=0;x<12;x++) {
		y+=mdays[x];
		if(y>z) {
			// within this month so quit
			month=months[x];
			x=12;
		}
	}
	return month;
}

function testNo(num) {
	var yr=num.charAt(0)*1;
	var wk=num.substring(1,3)*1 - 13;
	if(wk<13) {
		yr-=1;
		wk+=52;
	}
	var days=wk*7;
	var month = monthNo(days);
	alert(month+&quot;, &quot;+(2000+yr)+&quot;.&quot;);
}
testNo(&quot;237&quot;);
testNo(&quot;212&quot;);
testNo(&quot;437&quot;);
//-->
</script>

Pete.


Web Developer / CMS (Aptrix / LWWCM) Consultant
w: e: Pete.Raleigh(at)lclimited.co.uk
 
AWESOME- One more thing- I need to show what week it is, the month and year is great, I need what week it's equivalent to, like:

Week 24 of 2002 = June 2002

Thanks Man- AWESOME-
 
Here is my shortened solution:

Code:
function makeDate(_data)
{
	var myYear = parseInt(_data.substring(0,1));
	var myWeek = parseInt(_data.substring(1,3));

	myWeek -= 13;
	if (myWeek < 0)
	{
		myYear -= 1;
		myWeek += 52;
	}
	myYear += 2000;

	var theNewDate = new Date(new Date(myYear,0,1) - 0 + (myWeek * 604800000));
	return(monthArray[(theNewDate.getMonth())] + ' ' + myYear);
}

You can test it using the following:

Code:
<input type=&quot;text&quot; value=&quot;237&quot; onblur=&quot;alert(makeDate(this.value))&quot; size=&quot;3&quot;>

I think it does all you will need.

Jeff
 
OK... and updated to fulfill all your requirments (I hope):

Code:
function makeDate(_data)
{
	var myYear = parseInt(_data.substring(0,1));
	var myWeek = parseInt(_data.substring(1,3));

	myWeek -= 13;
	if (myWeek < 0)
	{
		myYear -= 1;
		myWeek += 52;
	}
	myYear += 2000;

	var theNewDate = new Date(new Date(myYear,0,1) - 0 + (myWeek * 604800000));
	return(&quot;Week &quot; + myWeek + &quot; of &quot; + myYear + &quot; = &quot; + monthArray[(theNewDate.getMonth())] + ' ' + myYear);
}

Jeff
 
Hi Jeff-

Thanks - I get MonthArray undefined!!
 
Hi Jeff,

I can't seem to figure out where the Month array is coming from; is it not there, or is it coming from code provided by someone else? That's the only thing that's missing, and it will be working; oh; how can I make it work by clicking a button instead of the &quot;OnBlur&quot; event?

Thanks,
Lisa
 
tsp1lrk,

Here you go with a button, pretty good, you started with
no solution and now you have 3 to choose from.

Code:
html><head><title>TEST</title>
<script language=&quot;JavaScript&quot;>
var months = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug',
  'Sep','Oct','Nov','Dec');
function cdwk(str){
wkmsec = 86400000 * 7;
wk = str.substr(1,2);
int_wk = parseInt(wk);
yrstr = &quot;200&quot; + str.substr(0,1);
int_yr = parseInt(yrstr);
wkc = int_wk - 13;
yrc = int_yr;
	if(wkc < 1){
	wkc = 52 + wkc;
	yrc = int_yr - 1;
	}
ndstr = &quot;Jan 1, &quot;+yrc;
nds = new Date(ndstr);
ndb = (nds - 0)+ (wkc * wkmsec);
ndal = new Date(ndb);
wknum = ((ndal - 0)-(nds - 0))/wkmsec;
mnth = months[ndal.getMonth()];
yr = ndal.getYear();
alert(wknum+' week of '+yr+' :: '+mnth+' '+yr);
}
</script></head><body>
<input type=&quot;text&quot; id=&quot;dt_str&quot; size=&quot;20&quot; value=&quot;237&quot;>
<input type=&quot;button&quot; value=&quot;Get Week&quot; 
onClick=&quot;cdwk(document.getElementById('dt_str').value)&quot;>
</body></html>

 
Sorry for the delay...

I forgot to post the monthArray code. It's a one liner just like the one that Lrnmore (and others) have posted. Here it is again (assuming you haven't already found a better solution):

Code:
var monthArray = new Array (&quot;January&quot;, &quot;February&quot;, &quot;March&quot;, &quot;April&quot;, &quot;May&quot;, &quot;June&quot;, &quot;July&quot;, &quot;August&quot;, &quot;September&quot;, &quot;October&quot;, &quot;November&quot;, &quot;December&quot;);

You would insert this line of code just before the function starts. You could put it inside the function too if you wanted.

All the best,
Jeff
 
Hello!

I can't thank all of you enough for all of your help with this, I don't know how to thank you! I gave you all stars! Thanks again- I may have one more problem I may need help with, but I'm going to try myself first to see if I can do it! ;>) If I get stuck, I'll post a note, I want to check out all this code and see if I can figure it out now- Thanks again-

;>)
Lisa
 
One more note, How did you all figure this out? Is there a particular resource or a Javascript book/website I could review and see? I really want to learn more about this; just not sure where to start- Thanks-

Lisa
 
I learnt mathematics at High School and University, if that helps. :) Seriously, it is less difficult (I won't say easy... geez, people will think I'm a geek otherwise) to break it down into smaller tasks, based on the information you know, and then build on that.

I'm a self taught HTML / JavaScript programmer, and have been learning on the job for years. Most of the JavaScript written on sites, is based around simply mathematics (for loops and conditions / comparisons).

One good site I've recently started to refer to is:

But I use &quot;O'Reilly - JavaScript: The Definitive Guide - 5th Edition&quot;, when I really get stuck and can't remember a method, function name, or object type. This book is more for medium to advanced users, once you understand and are comfortable programming JavaScript.

TIP: Use Google, and search for &quot;javascript ...&quot; and then some key terms you are trying to use. In your case, one starting point might have been &quot;javascript week of year&quot;. And then just check out the examples.

Hope this helps.

Pete.


Web Developer / CMS (Aptrix / LWWCM) Consultant
w: e: Pete.Raleigh(at)lclimited.co.uk
 
I started learning Javascript about 5 years ago. I began using a book titled: Javascript The Definitive Guide (published by O'Reilly). The version I have is now out of date... and rather than try to keep up with paper versions (that tend to gouge the back pocket sometimes) I use TekTips and Google for most of my stuff now.

I'd recommend taking problems that are posted here, reading the replies and trying to solve them yourself. Don't start with the hard stuff... there are plenty of simple things that would have a direct relevance on anyone's web development... email address validation, form submission, image rollovers.

I think the hardest thing is knowing the terminology... but trust me, that comes with time.

Jeff
 
tsp1lrk,

Glad to help also, I have been doing this for about
a year. As Jeff said this forum is the &quot;best&quot; way to learn.

I'll throw in one of my favorite resources, it includes
a &quot;zip&quot; version so that you can reference it locally.

[thumbsup2]


Great Javascript Resource:
 
AGGHH! Here is the other one, what do you guys think? I want to rip my hair out!

It's YYWW as in year (01-12),
week (01-52) 0303 = 2003 week 3 or January

Same deal-- enter in 3 digits click button and calculate week, month and year.

Lisa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top