...

View Full Version : Script to show a todays date + x months



JimboS
11-05-2006, 03:36 PM
I run a site which uses a regular payment scheme and I would like to it show a future date of today + 2 months on the page.

SO a payso payemnt is made today
next payment in 2 month (here I woudl liek to put a date of today + 2 months)

Any ideas?

James

brandonH
11-05-2006, 07:26 PM
<script type=text/javascript>
var dat=new Date();
var day=dat.getDate();
var year=dat.getYear();
var month=dat.getMonth()-(-1);
var todayis=month+"/"+day+"/"+year;
var addmonths=month-(-2);
var addyear=year-(-1);
if(addmonths>'12'){addmonths-=(12);
document.write("today is:"+todayis+"<br>your next bill is due:"+addmonths+"/"+day+"/"+addyear);
}
else{document.write('today is:'+todayis+'<br>your next bill is due:'+addmonths+'/'+day+'/'+year);}


</script>

JimboS
11-06-2006, 09:11 AM
thats works great - any idea how I could get the month to be showen as Jan/Feb etc rather than a number.

James:thumbsup:

Mr J
11-06-2006, 09:37 AM
In Firefox you get

today is:11/6/106
your next bill is due:1/6/107

For cross browser you should use

var year=dat.getFullYear();

instead of

var year=dat.getYear();

Kor
11-06-2006, 10:45 AM
You may use this:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="txt/javascript">
<script type="text/javascript">
var months=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
var today=new Date();
var tm=today.getMonth();
var td=today.getDate();
td<10?td='0'+td:null;
var ty=today.getFullYear();
var nextd=new Date(ty,tm+2,td);
var nm=nextd.getMonth();
var nd=nextd.getDate();
nd<10?nd='0'+nd:null;
var ny=nextd.getFullYear();
onload=function(){
var div=document.getElementById('mydiv');
var txt='Today is: '+months[tm]+' '+td+' '+ty+'<br>Your next bill is due: '+months[nm]+' '+nd+' '+ny;
div.innerHTML=txt;
}
</script>
</head>
<body>
<div id="mydiv"></div>
</body>
</html>

JimboS
11-06-2006, 09:56 PM
Genius guys - thanks

James

RobertR
03-09-2009, 08:11 PM
Hi
I have tried playing around with your code, but not being a programmer I haven't been able to achieve the result I want...

I would like a set of 12 checkboxes (4x3) with a month/year value for each one as follows:

<input type="checkbox" name="issue" VALUE="current_month">
<input type="checkbox" name="issue" VALUE="current_month+1">
<input type="checkbox" name="issue" VALUE="current_month+2">
<input type="checkbox" name="issue" VALUE="current_month...">

Date Format: Mar-2009, Apr-2009....

Can anyone help with the code for this plz

Philip M
03-09-2009, 09:10 PM
Why checkboxes? Surely only one choice can be selected, so what is needed is radio buttons.

RobertR
03-10-2009, 11:47 AM
Hi Philip
Well it's for an ad system and each customer can be in any one or months within a 12 month period, hence the checkbox solution. If you can think of a better solution I would really welcome your ideas
Thanks

Philip M
03-10-2009, 11:54 AM
If I understand you, the customer may elect to place an ad in one or more (not necessarly consecutive) monthly issues. OK?

So he can select one or more checkboxes labelled from Jan - Dec. OK?

What then is the output you desire, and how does it relate to the original question which was about displaying a date two months ahead?

Are you saying that you want the 12 checkboxes to start with the current month whatever that is, and display the names of the next 11 months thereafter? And what then after the boxes have been selected?

RobertR
03-10-2009, 12:08 PM
Hi Philip
Yes you have got it right, sorry for the confusion I was just using the idea as it was the closest to what I was looking for...
I have a database for customers at the moment and I want to add the 'ad database' each customer should be able to have selected any number of ads within the 12 months (including present month & not necessarly consecutive) the output will be added to the database in one field ',' seperated, field name 'issue'
Hope that makes sense

Philip M
03-10-2009, 01:15 PM
This should move you forward. But in future please start a new thread if the topic is a new one. This has little to do with the issues covered previously.


<form name = "myform">
JAN <input type = "checkbox" name = "chk1" id = "chk1" value= "Jan"><br>
FEB <input type = "checkbox" name = "chk2" id = "chk2" value = "Feb"><br>
MAR <input type = "checkbox" name = "chk3" id = "chk3" value = "Mar"><br>

<input type = "button" name = "but1" value = "Show Selections" onclick = "showsel()">
</form>

<script type = "text/javascript">

function showsel() {
var res = "You are placing ads in the following issues:- ";
var len = res.length;
var choice = "";
var x = "";
for (var i=1; i<=3; i++) { // change 3 to 12 for full version
x = eval("document.myform.chk" + i + ".value");
y = eval("document.myform.chk" + i + ".checked");
if (eval("y")) {
choice = choice + " " + x + ","; // comma added after each choice
}
}
res = res + choice;
res = res.replace(/\,$/,""); // remove final comma in choice list
if (res.length <= len ) {
res = res + "NONE";
}

alert (res);
}

</script>

RobertR
03-10-2009, 01:40 PM
Hi Philip
Oops! sorry about that I will make sure in future I post a new thread. thanks for your help and advice...
The script does almost exactly what I was looking for except the first month needs to automatically be current month and then the following checkboxes add 1 month and so on... any ideas?
Many thanks again

Philip M
03-10-2009, 01:50 PM
It seems to be your lucky day. ;) Here is an expanded script which lists the 12 checkboxes in month order starting with the current month, and adds the current year or next year as appropriate. I do hope that you will not now want to change the specification! ("Oh, it works well. But what I really wanted was .....")



<form name = "myform">
<script type = "text/javascript">
var now = new Date();
var nowmonth = now.getMonth() + 1;
var nowyear = now.getFullYear();

var mth1 = 'JAN <input type = "checkbox" name = "chk1" id = "chk1" value= "Jan"><br>'
var mth2 = 'FEB <input type = "checkbox" name = "chk2" id = "chk2" value = "Feb"><br>'
var mth3 = 'MAR <input type = "checkbox" name = "chk3" id = "chk3" value = "Mar"><br>'
var mth4 = 'APR <input type = "checkbox" name = "chk4" id = "chk4" value = "Apr"><br>'
var mth5 = 'MAY <input type = "checkbox" name = "chk5" id = "chk5" value = "May"><br>'
var mth6 = 'JUN <input type = "checkbox" name = "chk6" id = "chk6" value = "Jun"><br>'
var mth7 = 'JUL <input type = "checkbox" name = "chk7" id = "chk7" value = "Jul"><br>'
var mth8 = 'AUG <input type = "checkbox" name = "chk8" id = "chk8" value = "Aug"><br>'
var mth9 = 'SEP <input type = "checkbox" name = "chk9" id = "chk9" value = "Sep"><br>'
var mth10 = 'OCT <input type = "checkbox" name = "chk10" id = "chk10" value = "Oct"><br>'
var mth11 = 'NOV <input type = "checkbox" name = "chk11" id = "chk11" value = "Nov"><br>'
var mth12 = 'DEC <input type = "checkbox" name = "chk12" id = "chk12" value = "Dec"><br>'

for (var i = nowmonth; i <= 12; i++) {
document.write(eval("mth"+ i));
}
for (var i = 1; i < nowmonth; i++) {
document.write(eval("mth"+ i));
}
</script>

<input type = "button" name = "but1" value = "Show Selections" onclick = "showsel()">
</form>

<script type = "text/javascript">

function showsel() {
var res = "You are placing ads in the following issues:- ";
var len = res.length;
var choice = "";
var x = "";
for (var i=nowmonth; i<=12; i++) {
x = eval("document.myform.chk" + i + ".value");
y = eval("document.myform.chk" + i + ".checked");
if (eval("y")) {
choice = choice + " " + x + " " + nowyear + ",";
}
}
for (var i=1; i<nowmonth; i++) {
x = eval("document.myform.chk" + i + ".value");
y = eval("document.myform.chk" + i + ".checked");
if (eval("y")) {
choice = choice + " " + x + " " + (nowyear+1) + ",";
}
}

res = res + choice;
res = res.replace(/\,$/,"");
if (res.length <= len ) {
res = res + "NONE";
}
alert (res);
}

</script>

RobertR
03-10-2009, 02:05 PM
Hi Philip
That works great thank you very much! and no changes needed hehe

Kor
03-10-2009, 02:24 PM
Why eval()? Why not simply, for instance?:


x = document.myform['chk' + i].value;
...

Henley
03-10-2009, 02:34 PM
Or, using DOM methods, and valid mark-up:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>
Publication Month Checkboxes
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">


function validate(nForm){

var nIssue = document.getElementsByName('issue');
var temp = [];
for (i=0; i<nIssue.length; i++)
{
if (nIssue[i].checked)
{
temp[temp.length] = nIssue[i].value;
}
}
if (temp.length == 0)
{
alert('You must select publication month(s)');
return false;
}
alert('Thank you. You chose:\n' + temp.join('\n'));
return true;
}

function init(){

var monthName = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var refDate = new Date();
refDate.setHours(0,0,0,0);
var nIssue = document.getElementsByName('issue');
for (i=0; i<nIssue.length; i++)
{
nIssue[i].parentNode.firstChild.data = monthName[refDate.getMonth()] + " " + refDate.getFullYear();
nIssue[i].value = monthName[refDate.getMonth()] + " " + refDate.getFullYear();
refDate = new Date(refDate.getFullYear(),refDate.getMonth()+1,1);
}
}

navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);

</script>
<style type="text/css">

body {background-color: #fffacd; margin-top: 60px;}
form {width: 620px; margin: auto; font-family: 'times new roman'; font-size: 12pt;}
fieldset {width: 620px; padding-left: 10px; background-color: #eee8aa; border: 1px solid #e6b280;}
legend {font-family: arial; font-size: 14pt; color: #000000; background-color: transparent; padding-top: 3px;
padding-left: 3px; padding-right: 3px; margin-bottom: 5px;}
.issueLabel {font-family: tahoma; font-size: 10pt; margin-right: 10px;}
.submitBtn {background-color: #fff8dc; border: 1px solid #000000; font-family: arial; font-size: 10pt;
font-weight: bold; cursor: pointer; display: block; margin-left: auto; margin-right: auto;
margin-top: 5px; margin-bottom: 5px;}

</style>
</head>

<body>
<form action="" method="post" onsubmit="return validate(this)">
<fieldset>
<legend>Form</legend>
<label>Select Issue Publication Month(s)</label>
<br>
<label class="issueLabel">&nbsp;<input type="checkbox" name="issue"></label>&nbsp;&nbsp;&nbsp;
<label class="issueLabel">&nbsp;<input type="checkbox" name="issue"></label>&nbsp;&nbsp;&nbsp;
<label class="issueLabel">&nbsp;<input type="checkbox" name="issue"></label>&nbsp;&nbsp;&nbsp;
<label class="issueLabel">&nbsp;<input type="checkbox" name="issue"></label>&nbsp;&nbsp;&nbsp;
<br>
<label class="issueLabel">&nbsp;<input type="checkbox" name="issue"></label>&nbsp;&nbsp;&nbsp;
<label class="issueLabel">&nbsp;<input type="checkbox" name="issue"></label>&nbsp;&nbsp;&nbsp;
<label class="issueLabel">&nbsp;<input type="checkbox" name="issue"></label>&nbsp;&nbsp;&nbsp;
<label class="issueLabel">&nbsp;<input type="checkbox" name="issue"></label>&nbsp;&nbsp;&nbsp;
<br>
<label class="issueLabel">&nbsp;<input type="checkbox" name="issue"></label>&nbsp;&nbsp;&nbsp;
<label class="issueLabel">&nbsp;<input type="checkbox" name="issue"></label>&nbsp;&nbsp;&nbsp;
<label class="issueLabel">&nbsp;<input type="checkbox" name="issue"></label>&nbsp;&nbsp;&nbsp;
<label class="issueLabel">&nbsp;<input type="checkbox" name="issue"></label>&nbsp;&nbsp;&nbsp;

<input type="submit" name="submit" value="Submit" class="submitBtn">
</fieldset>
</form>
</body>
</html>

Philip M
03-10-2009, 02:47 PM
That's great, Cranford - er, I mean Henley. ;)

RobertR
03-10-2009, 06:42 PM
Hi Henley
Thanks for the code! also works great
Cheers
Rob

RobertR
03-14-2009, 06:28 PM
Hi Again

Sorry to bother you all again with this code, but I have tried to sort the problem myself without any luck! I have sucessfully manage to get your code to work with my DB, however when the data selection is written to the database each result is separated by
by '~~' May 2009~~Apr 2009 when I would like it to show as May 2009,Apr 2009. I believe this code will work: $rec{'issue'} =~ s/\,/, /g; but I am not sure where should be placed in your code to work

Many Thanks

Rob

RobertR
03-14-2009, 06:32 PM
Alternatively, is there a way to get each month to have the refer of issue1, issue2 and so on... This would be the preferred result.

Thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum