Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    New Coder
    Join Date
    Oct 2002
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Script to show a todays date + x months

    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

  • #2
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    Code:
    <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>
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #3
    New Coder
    Join Date
    Oct 2002
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thats works great - any idea how I could get the month to be showen as Jan/Feb etc rather than a number.

    James

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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();
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You may use this:
    Code:
    <!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>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    New Coder
    Join Date
    Oct 2002
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Genius guys - thanks

    James

  • #7
    New Coder
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    today's date + x months in check box (12 months)

    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

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,240
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    Why checkboxes? Surely only one choice can be selected, so what is needed is radio buttons.

  • #9
    New Coder
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,240
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    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?
    Last edited by Philip M; 03-10-2009 at 11:57 AM.

  • #11
    New Coder
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,240
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    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.

    Code:
    <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>
    Last edited by Philip M; 03-10-2009 at 01:22 PM.

  • #13
    New Coder
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,240
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    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 .....")


    Code:
    <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>
    Last edited by Philip M; 03-10-2009 at 01:53 PM. Reason: Typo

  • Users who have thanked Philip M for this post:

    RobertR (03-10-2009)

  • #15
    New Coder
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi Philip
    That works great thank you very much! and no changes needed hehe


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •