SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to get Mondays Date of chosen date

    Hello all,
    I am giving a date as an input and i am looking the date of monday(Before) of that week.
    How can i get monday date on the respective given date.

    Thanks
    Ujjwal
    -::Ujjwal::-
    Visit My Site

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,688
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Find out how far away monday is, and subtract the difference.
    Code javascript:
    var date = new Date('2008/07/30');
    var today = date.getDay();
    var daysToMonday = today - 1;
    var monday = new Date();
    monday.setDate(date.getDate() - daysToMonday);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.
    But it is not fully works.
    var dayGiven=document.frmStatusReport.txtPeriodDate.value;
    var date = new Date(eval('"'+dayGiven+'"'));
    var today = date.getDay();
    var daysToMonday = today - 1;
    var monday = new Date();
    monday.setDate(date.getDate() - daysToMonday);
    document.frmStatusReport.txtDate2.value=monday.getMonth()+"/"+monday.getDate()+"/"+monday.getFullYear();


    and if i am giving "07/25/2008" as an date it gives "06/21/2008" where as it should show
    "07/21/2008"
    -::Ujjwal::-
    Visit My Site

  4. #4
    SitePoint Guru SSJ's Avatar
    Join Date
    Jan 2007
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to debug the code here. Try the alerts and get which value you are getting.

  5. #5
    SitePoint Guru ujjwal's Avatar
    Join Date
    Oct 2004
    Location
    kolkata
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes i debug the code and monday.getMonth() function returns 6 instead of 7.

    document.frmStatusReport.txtDate2.value=monday.getMonth()+"/"+monday.getDate()+"/"+monday.getFullYear();


    and also i wonder if i give "01/14/2008" as an input date it gives wrong data.
    -::Ujjwal::-
    Visit My Site

  6. #6
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    0 january
    1 february
    2 march
    3 april
    4 may
    5 june
    6 july
    Code javascript:
    <script type="text/javascript">
     
    function f(){
    var el= document.getElementById('inp1');
    var s= el.value.split(/\D+/);
    // alert(s);  // 07,25,2008
    // alert(s[2]); // year  2008
    //alert(s[0]); // month  07
    // alert(s[1]); // day     25 
    // var d= new Date(year,month-1, day); 
    var d= new Date(s[2]*1,s[0]-1,s[1]*1);
    // alert(d)
    // alert(d.getDay());
    // alert( d.getDay()-1  );
    d.setDate(d.getDate()-(d.getDay()-1)) ;
    // alert(d);
    var el2 = document.getElementById('inp2');
    var m=d.getMonth() + 1;
    var gm= (m<10) ? "0" + m  : m;
    // alert(gm);
    var gd = (d.getDate() <10 ) ? "0" + d.getDate()  : d.getDate();
    // alert(gd);
    el2.value= gm+"/"+gd+"/"+d.getFullYear() ;
    }
     
    </script>
    </head>
    <body>
    <input type="text" value="07/25/2008" id="inp1"><br>
    <input type="button" value="click me" onclick="f()"><br>
    Monday: <input type="text" id="inp2">
    or
    Code javascript:
    <script type="text/javascript">
     
    function f(){
    var el= document.getElementById('inp1');
    var d= new Date(el.value);
    // alert(d);
    d.setDate(d.getDate()-(-1 + d.getDay())) ;
    var el2 = document.getElementById('inp2');
    // alert(d.getMonth());
    var m=d.getMonth()+1 ;
    var gm= (m<10) ? "0" + m  : m;
    var gd = (d.getDate() <10 ) ? "0" + d.getDate()  : d.getDate();
    el2.value= gm+"/"+gd+"/"+d.getFullYear() ;
    }
     
    </script>
    </head>
    <body>
    <input type="text" value="07/25/2008" id="inp1"><br>
    <input type="button" value="click me" onclick="f()"><br>
    Monday: <input type="text" id="inp2">
    http://www.w3schools.com/jsref/jsref_obj_date.asp
    http://lawrence.ecorp.net/inet/samples/js-date-fx.shtml
    Last edited by muazzez; Jul 30, 2008 at 13:02.
    The Time Through Ages
    1. By the Time, 2. Verily Man is in loss, 3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.


Bookmarks

Posting Permissions

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