0

I have two text boxes which hold from date and to date in my page. I have some buttons. When I click on the button 1day, the from date and to date should be set to today's date. When I click on the button 1 week, to date should be set to today's date and from date should be set to 1 week before today. Other buttons have similar functionality. How can I do that using jQuery or JavaScript? Please provide me with a FIDDLE

            <div id="date">
              From: <input  type="text" size="6" name="date" id="from" class="tcal">
              To:   <input type="text" size="6" name="date1" id="to" class="tcal">
              <button id="1day"> 1 Day </button>
              <button id="month"> 1 Month</button>
              <button id="3month"> 3 Month</button>
              <button id="year"> 1 year</button>
            </div>
Anusha Honey
  • 887
  • 4
  • 11
  • 26

2 Answers2

1

Use date Object

For ex :

$("#1day").click(function () {
$("#from").val(new Date().toString());
$("#to").val(new Date().toString()) ;
});

http://jsfiddle.net/sureshbm13/DWUrh/7/

To move further read :

Related to format :

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

Try this code:

Look at this demo -

DEMO jsfiddle

   $('button').click(function(){
       id = $(this).attr('id');
       $('#to').val((new Date()).getMonth() + "-" + (new Date()).getDate() + "-" + (new Date()).getFullYear());   

       if(id == '1day'){
           $('#from').val((new Date()).getMonth() + "-" + ((new Date()).getDate()-1) + "-" + (new Date()).getFullYear());          
       }
       if(id == 'month'){
           $('#from').val(((new Date()).getMonth()-1) + "-" + (new Date()).getDate() + "-" + (new Date()).getFullYear());     
       }

       if(id == '3month'){
           $('#from').val(((new Date()).getMonth()-3) + "-" + (new Date()).getDate() + "-" + (new Date()).getFullYear());     
       }

       if(id == 'year'){
          $('#from').val((new Date()).getMonth() + "-" + (new Date()).getDate() + "-" + ((new Date()).getFullYear()-1));     
       }
  });
Kailash Ahirwar
  • 771
  • 3
  • 14