www.webdeveloper.com
Results 1 to 4 of 4

Thread: use javascript to check if the date is a weekend or public holiday

  1. #1
    Join Date
    Mar 2013
    Posts
    2

    use javascript to check if the date is a weekend or public holiday

    Hi

    I have a datetimepicker -jqueryui, just wondering if is it possible for me to check if the date selected is a weekend or public holiday. If its a public holiday,can I have like a text file with a list of public holidays that it can cross check with.

    I would like to have something like this.image1.png

    The from and to time is enabled with datetimepicker and the "hour difference between the from and to time" calculated will be entered into the overtime total hours. If the date selected is a normal day, it will send the value to the normal day textbox, if its a weekend, it will send the value to the rest day textbox and if its a public holiday, it will send the value to the public holiday text box.

    Any idea or guidance.

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,436

    Lightbulb

    You can use the Date() function to determine weekends...
    Code:
    <script type="text/javascript">
    var then = new Date(2013,2,2);  // a Saturday  (March 2, 2013)
    alert(then.getDay()); // show 6
    then = new Date(2013,2,3);  // a Sunday  (March 3, 2013)
    alert(then.getDay()); // show 0
    </script>
    getDay() returns 0=Sunday thru 6=Saturday

  3. #3
    Join Date
    Mar 2013
    Posts
    2

    Got that...how do I copy the value to the next textbox

    Hi, thanks

    how do i get the value to appear into the next textbox instead of using the alert message, this is my code

    Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script type="text/javascript">
    
    function myFunction()
    {
        //var d = new Date();
        var y = document.getElementById("TextBox2").value;
        var x = new Date(document.getElementById("TextBox1").value);
        return x;
        y = x.getDay();
        //alert(x.getDay());
    
    }
    
    </script>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:TextBox ID="TextBox1" runat="server" value="03/16/2013"></asp:TextBox>
            <button onclick="myFunction()">Try it</button>
    
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        </div>
        </form>
    </body>
    </html>

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,436

    Lightbulb

    Quote Originally Posted by kalpna0611 View Post
    Hi, thanks

    how do i get the value to appear into the next textbox instead of using the alert message, this is my code

    Code:
    ...
    I know nothing about the 'asp:' stuff you have in your example,
    so if it is important you will need to add it back in.

    For a pure JS solution (without using JQuery), consider this...
    Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    
    <script type="text/javascript">
    function myFunction(tb1,tb2) {
      var tarr = document.getElementById(tb1).value.split('/');
      var d = new Date(tarr[2],tarr[0]-1,tarr[1]);
      msg = 'Date is a workday';
      if (d.getDay() == 0) { msg = 'Date is a Sunday'; }
      if (d.getDay() == 6) { msg = 'Date is a Saturday'; }
      document.getElementById(tb2).value = msg;
    }
    </script>
    </head>
    <body>
    <form id="form1" action="" method="post" onsubmit="return false"> <!-- removed for testing: runat="server" -->
    <div>
                
     <input ID="TextBox1" value="03/16/2013">
     <button onclick="myFunction('TextBox1','TextBox2')">Try it</button>
     <input ID="TextBox2" value="">
    
    </div>
    </form>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles