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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript and some working out dates fun

Status
Not open for further replies.

matc

Programmer
Feb 11, 2002
5
GB
Hello all,

If anyone can help I just have something that I'm finding hard to resolve. I'm a little confused about how best to do this without makng a mess of what I've already done or making to many confusing references or it too hard to maintain. It took me a while to get this far as it is...

Here are some of the more annoying problems with the script. I would really appricate some expert help with resolving:

1. If its the first of the month I should really get the last day of the month before, contained and grabbed from present months (august) monthdays var: (This previous months days are stored in the presebnt months monthdays var. Seem easier at the time to do it that way.)

if (daym == 1 && monthstr == "August") {
ddb4 = monthdays;
lmm = "0" + (month(-1));
}

Now if its the 2nd day in the present month it returns the right day number but not the correct present month we're in but the month before, i.e, 07 instead of 08. Please can anyone help me get away from this happening?

2. If its New Years Day on this year the script always returns 04 and also simply 00 for the month. For example if i'm on Local Time of 1st january 04 it returns 310004 which is not that great.
Now I would love it to show 03 for the year and 12 for the month instead. I know I haven't attemped to coded that in but I'm not sure how to structure that yet properly.

Also please if you spot anything else that may need improving with this script please do let me know :)

If anyone needs me to go over anything in more detail I am more than happy to do so as I really want to get this script fixed. Its really been opushing me to the edge here.

Heres the fun part anyway:

<script language="Javascript" type="text/javascript">
<!--
var today = new Date(); //create a master date object held with the variable 'today'.
var year = today.getFullYear(); //extract the full year from todays date object. (yyyy)
var month = today.getMonth()+1; //extract the month from todays date object. (1-12)
var daym = today.getDate(); //extract the day of month from todays date object. (1-31)
var lastmonth = today.getMonth(); //extract the month from todays date object. (1-12)
var lastyear = today.getFullYear(); //extract the full year from todays date object. (yyyy)
trimYear = year.toString().substr(2,2); //from 4 digits trim the year string down to the more useful last 2 digits.

//THIS year, month and day statements.

if (year <1000) //incompatible year fix, take the year and add 1900 to help cover some versions of Gecko.
year += 1900

mm = month; //this month (format: mm).
if (mm<10) //if the month is less than 2 gigits (i.e. <10) prefix the value with a "0".
mm = "0"+mm

dd = daym; //todays day (format: dd).
if (dd<10) //if the day is less than 2 gigits (i.e. <10) prefix the value with a "0".
dd = "0"+dd

//LAST year, month and day statements.

ddb4 = daym-1; //yesterdays day (format: dd).
if (ddb4<10) //in addition to getting todays day lets engineer the date object to get the previous day.
ddb4 = "0"+ddb4 //this is useful for the first of each month by returning the last day of the previous month.

lmm = lastmonth; //last month (format: mm).
if (lmm<10) //same as above but usuful for engineering the previous month when requesting the last day of that previous month.
lmm = "0"+lmm //if the month is less than 2 gigits (i.e. <10) prefix the value with a "0".

//:required: monthstr, catalogue the months in a year and assign a numbered month to the plain english word.
if (month == 1) var monthstr = "January";
if (month == 2) var monthstr = "February";
if (month == 3) var monthstr = "March";
if (month == 4) var monthstr = "April";
if (month == 5) var monthstr = "May";
if (month == 6) var monthstr = "June";
if (month == 7) var monthstr = "July";
if (month == 8) var monthstr = "August";
if (month == 9) var monthstr = "September";
if (month == 10) var monthstr = "October";
if (month == 11) var monthstr = "November";
if (month == 12) var monthstr = "December";

//:required: monthdays, catalogue the days of each month and assign them to the actual months defined above.
if (monthstr == "January") var monthdays = "31";
if (monthstr == "February") var monthdays = "28";
if (monthstr == "March") var monthdays = "31";
if (monthstr == "April") var monthdays = "30";
if (monthstr == "May") var monthdays = "29";
if (monthstr == "June") var monthdays = "30";
if (monthstr == "July") var monthdays = "31";
if (monthstr == "August") var monthdays = "31";
if (monthstr == "September") var monthdays = "30";
if (monthstr == "October") var monthdays = "29";
if (monthstr == "November") var monthdays = "30";
if (monthstr == "December") var monthdays = "31";

//each of the following statements only come into play on the first the month and only if desired.
//these statments are used in conjuction with the monthstr and monthdays declarations to fix any 1st of the month issues.
if (daym == 1 && monthstr == "January") {
ddb4 = monthdays;
lmm = "0" + (month(-1));
}
if (daym == 1 && monthstr == "February") {
ddb4 = monthdays;
lmm = "0" + (month(-1));
}
if (daym == 1 && monthstr == "March" && year %3 !== 0) {
ddb4 = monthdays;
lmm = "0" + (month(-1));
}
if (daym == 1 && monthstr == "March" && year %3 == 0) {
ddst = 29;
lmm = "0" + (month(-1));
}
if (daym == 1 && monthstr == "April") {
ddb4 = monthdays;
lmm = "0" + (month(-1));
}
if (daym == 1 && monthstr == "May") {
ddb4 = monthdays;
lmm = "0" + (month(-1));
}
if (daym == 1 && monthstr == "June") {
ddb4 = monthdays;
lmm = "0" + (month(-1));
}
if (daym == 1 && monthstr == "July") {
ddb4 = monthdays;
lmm = "0" + (month(-1));
}
if (daym == 1 && monthstr == "August") {
ddb4 = monthdays;
lmm = "0" + (month(-1));
}
if (daym == 1 && monthstr == "September") {
ddb4 = monthdays;
lmm = "0" + (month(-1));
}
if (daym == 1 && monthstr == "October") {
ddb4 = monthdays;
lmm = "0" + (month(-1));
}
if (daym == 1 && monthstr == "November") {
ddb4 = monthdays;
lmm = "0" + (month(-1));
}
if (daym == 1 && monthstr == "December") {
ddb4 = monthdays;
lmm = month;
}

//-->
</script>

---------------------------------------------
HTML Stuff:

<script language="Javascript" type="text/javascript">
<!--
document.write ("Todays date is: <strong>"+ dd + mm + trimYear +"</strong>")
//-->
</script>
<br /><br />
<script language="Javascript" type="text/javascript">
<!--
document.write ("Yesterdays date was: <strong>"+ ddb4 + lmm + trimYear +"</strong>")
//-->
</script>


Really many, many thanks for help offered.

matc
 
Function below

<script LANGUAGE="JavaScript">

function DateAdd(startDate, numDays, numMonths, numYears)
{
var returnDate = new Date(startDate.getTime());
var yearsToAdd = numYears;

var month = returnDate.getMonth() + numMonths;
if (month > 11)
{
yearsToAdd = Math.floor((month+1)/12);
month -= 12*yearsToAdd;
yearsToAdd += numYears;
}
returnDate.setMonth(month);
returnDate.setFullYear(returnDate.getFullYear() + yearsToAdd);

returnDate.setTime(returnDate.getTime()+60000*60*24*numDays);

return returnDate;

}

function YearAdd(startDate, numYears)
{
return DateAdd(startDate,0,0,numYears);
}

function MonthAdd(startDate, numMonths)
{
return DateAdd(startDate,0,numMonths,0);
}

function DayAdd(startDate, numDays)
{
return DateAdd(startDate,numDays,0,0);
}
</script>
 
Hi shatch and thank you for replying so fast,

I haven't looked that closely at your script yet but I'll quickly add something about what I'm going to be using the other script for. This will make its application a bit clearer, and probably explain why I'm going through so much pain at the moment :) Also please forgive me if you've already provided a more shore fire way of doing this I will of course spend the time in a bit to check what you've submitted.

Anyway for the minute I was hoping to have this script used for a number of different pages/applications. The most important of these is to have it fired via a simply function that passed two simply paras to the script and produced the correct dynmically formatted date into a image soure like below:

... onclick="getImg(summary,sum1); return false;" ...

...summary being the image id and sum1 being one of a number of seperate vars used to replace the original window.load image with.

e.g: var sum1 ='/summary/yesterday"+ ddb4 + lmm + trimYear +"img.jpg';

This would hopefully make the script very stand alone and make it much easier when maintaining.

cheers, mat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top