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
 
Try:
Code:
<html>
<head>
<title>Make a date</title>
</head>
<body>

<script language=&quot;JavaScript&quot;>
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;);

function makeDate(_data) {
    var myYear = parseInt(_data.substring(0,2));
    var myWeek = parseInt(_data.substring(2,4));

    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);
}
document.write(makeDate(&quot;0303&quot;));
</script>

</body>
</html>

Thanks to Jeff. [thumbsup2]

Pete.


Web Developer / CMS (Aptrix / LWWCM) Consultant
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Thanks--- ;>)

now how do I add the button deal and all so they can enter in their number and click calculate giving them that info? Man, I owe you all, I really do appreciate your time and resources, I again, can't thank you enough. I just need to understand this stuff.

Lisa
 
Lisa...

Remove the line in WarTookMan's code sample that reads document.write(makeDate(&quot;0303&quot;));. Then add a button or something on your HTML page like this:

Code:
<p><input type=&quot;text&quot; id=&quot;myCode&quot; size=&quot;4&quot;>  <input type=&quot;button&quot; value=&quot;Check Code&quot; onclick=&quot;makeDate(document.getElementById('myCode').value)&quot;></p>


That will let you call the code with your own numbers.

Jeff
 
Hey!!!

I got it before I even read this post!! ;>) Thanks- I'm sorry. I looked at the other one and figured it out- Thanks again for your quick responses, I can always rely on this forum for friendly assistance.

Lisa
 
I think you've already said enough with:
&quot;...I really do appreciate your time and resources, I again, can't thank you enough...&quot;.

Glad we could help.

Pete.


Web Developer / Aptrix CMS (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Hey There!

How can I add to the Week part of this code, it to read, st, for 1st, 22nd for 22, 15th with the th; I know there is a way to add it- but I'm having trouble; here is the orginal script:

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



result.innerHTML= wknum+' week of '+yr+' :: '+mnth+' '+yr
}

</script>

Here is a script I found; I've changed it around a bit to no avail:

if (date == 1 || date == 21 || date == 31)
{ender = &quot;<SUP>st</SUP>&quot;}

else
if (date == 2 || date == 22)
{ender = &quot;<SUP>nd</SUP>&quot;}

else
if (date == 3 || date == 23)
{ender = &quot;<SUP>rd</SUP>&quot;}

else
{ender = &quot;<SUP>th</SUP>&quot;}

Thanks
 
There is a fancy rule (where you default to TH and then replace with RD or ND on exception)... but it's just as easy to use an array.

Code:
var dateExtn = new Array('','st','nd','rd','th','th','th','th','th','th','th','th','th','th','th','th','th','th','th','th','th','st','nd','rd','th','th','th','th','th','th','th','st');

You would then just access the correct index by using the date (ie: if the date is 21, dateExtn[21] returns 'st'). Since arrays are usually zero based, I have padded out the 0th element of the array with an empty string to make indexing more optimal.

All the best,
Jeff
 
Hi there!

It's me again! I have another request- this seems fairly okay; I'm trying to figure the logic for this one, here is my current code for figuring the date, which is working nicely:

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



result.innerHTML= wknum+' week of '+yr+' : '+mnth+' '+yr
}

</script>

_________________________________________________

Now-- after plugging in the numbers, we get the date of manufacturer. We want to figure if it's in or out of warranty. It's calculation is 9 months from the Date of Manufacturer. Say after plugging in our numbers from the above code, we get:

Oct 2003

1) Using the current date
2) Use 9 months from Date of Manufacturer
3) Is the Manufacturer date less than 9 months? Then it's IN warranty
4) If it's more than 9 months, it's OUT of warranty.

Can anyone help me?
Thanks,
Lisa
 
Hi tsp1lrk,

See if this will do, I'm sure someone here can &quot;tune&quot; it
up for you.

Shout back if it needs any adjustment.

Code:
<html><head>
  <title></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;
mnths9 = 86400000 * 270; // 270 days
nowDt = new Date();
warr = &quot;Item not under warranty..&quot;;
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();
result.innerHTML= wknum+' week of '+yr+' : '+mnth+' '+yr
if((((ndal-0)+(mnths9-0)) > (nowDt-0))) {
 warr = &quot;Item is under warranty..&quot;; }
warrtxt.innerHTML = warr;
}

</script></head><body>
<input type=&quot;text&quot; size=&quot;20&quot; onBlur=&quot;cdwk(this.value)&quot; 
value=&quot;137= 2(2002)&quot;> 
<div id='result'>Output</div>
<div id='warrtxt'>Warranty</div>
</body></html>

 
THAT IS AWESOME- it worked- THANK YOU- I forgot I have one more script- I should have added it before; same thing applies with the warranty stuff:

<script language=&quot;JavaScript&quot;>
<!--

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

function makeDate(_data) {
var myYear = parseInt(_data.substring(0,2));
var myWeek = parseInt(_data.substring(2,4));

myYear += 2000;

var theNewDate = new Date(new Date(myYear,0,1) - 0 + (myWeek * 604800000));
result.innerHTML=(&quot;Week &quot; + myWeek + &quot; of &quot; + myYear + &quot; = &quot; + monthArray[theNewDate.getMonth()] + ' ' + myYear + ' ' + &quot;which is the Date of Manufacturer.&quot; )
}
//-->
</script>

Need the 9 month thing with in or out of warranty based on 9 months from the date returned by this script.

Thanks-
Lisa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top