new Date() problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • risk32
    New Member
    • Mar 2008
    • 98

    new Date() problem

    Hi all. I'm having a little bit of a problem with the new Date() object in JS. I can get it to work just fine, however, I don't want the .getDay() to return a number. I started to create an array with the days of the week, but I wasn't sure how to use it, so I removed it from my code. I'm trying to make an alarm clock with JS.
    I'll put my code in two separate instances, one for HTML and one for my .js file.

    JS
    Code:
    // variables
    
    var day = document.getElementById("day");           //day
    var hour = document.getElementById("hour");        // hour
    var minute = document.getElementById("minute");   // minute
    
    var path = document.getElementById("path"); // path of the sound file
    
    function time()
    {
    
    var time = new Date();
    var dayNow = time.getDay();
    var hourNow = time.getHours();
    var minuteNow = time.getMinutes();
    var am = "AM";
    var pm = "PM";
    
    if (hourNow > 12)
     {
       hour = hour - 12;
    document.getElementById("time").value = 'Current time is ' + dayNow + ', ' + hourNow + ':' + minuteNow + ' ' + pm;
     }
    
    else
     {
       document.getElementById("time").value = 'Current time is ' + dayNow + ', ' + hourNow + ':' + minuteNow + ' ' + am;  
     }
    
    
    setTimeout("time()",1000);
    
    }
    HTML
    Code:
    <html>
    <head>
    <script type="text/javascript" src="alarmclock.js"></script>
    </head>
    <body onload="time();">
    
    <table bgcolor="gray" width=300>
    
    <td><font color="white">
    <center>
    <textarea id="time" style="border:none; overflow:auto"></textarea>
    <br>
    Select the day you want the alarm to run.<br>
    <select>
    <option name="sunday" value="sunday" id="day">Sunday
    <option name="monday" value="monday" id="day">Monday
    <option name="tuesday" value="tuesday" id="day">Tuesday
    <option name="wednesday" value="wednesday" id="day">Wednesday
    <option name="thursday" value="thursday" id="day">Thursday
    <option name="friday" value="friday" id="day">Friday
    <option name="saturday" value="saturday"id="day" >Saturday
    </select>
    </center>
    
    <center>
    Select the time you want the alarm to run.<br>
    
    <select>
    <option name="1" value="1" id="hour">1
    <option name="2" value="2" id="hour">2
    <option name="3" value="3" id="hour">3
    <option name="4" value="4" id="hour">4
    <option name="5" value="5" id="hour">5
    <option name="6" value="6" id="hour">6
    <option name="7" value="7" id="hour">7
    <option name="8" value="8" id="hour">8
    <option name="9" value="9" id="hour">9
    <option name="10" value="10" id="hour">10
    <option name="11" value="11" id="hour">11
    <option name="12" value="12" id="hour">12
    </select>
    
    <select>
    <option name="00" value="00" id="minute">00
    <option name="01" value="01" id="minute">01
    <option name="02" value="02" id="minute">02
    <option name="03" value="03" id="minute">03
    <option name="04" value="04" id="minute">04
    <option name="05" value="05" id="minute">05
    <option name="06" value="06" id="minute">06
    <option name="07" value="07" id="minute">07
    <option name="08" value="08" id="minute">08
    <option name="09" value="09" id="minute">09
    <option name="10" value="10" id="minute">10
    
    <option name="11" value="11" id="minute">11
    <option name="12" value="12" id="minute">12
    <option name="13" value="13" id="minute">13
    <option name="14" value="14" id="minute">14
    <option name="15" value="15" id="minute">15
    <option name="16" value="16" id="minute">16
    <option name="17" value="17" id="minute">17
    <option name="18" value="18" id="minute">18
    <option name="19" value="19" id="minute">19
    <option name="20" value="20" id="minute">20
    
    <option name="21" value="1" id="minute">21
    <option name="22" value="2" id="minute">22
    <option name="23" value="3" id="minute">23
    <option name="24" value="4" id="minute">24
    <option name="25" value="5" id="minute">25
    <option name="26" value="6" id="minute">26
    <option name="27" value="7" id="minute">27
    <option name="28" value="8" id="minute">28
    <option name="29" value="9" id="minute">29
    <option name="30" value="0" id="minute">30
    
    <option name="31" value="31" id="minute">31
    <option name="32" value="32" id="minute">32
    <option name="33" value="33" id="minute">33
    <option name="34" value="34" id="minute">34
    <option name="35" value="35" id="minute">35
    <option name="36" value="36" id="minute">36
    <option name="37" value="37" id="minute">37
    <option name="38" value="38" id="minute">38
    <option name="39" value="39" id="minute">39
    <option name="40" value="40" id="minute">40
    
    <option name="41" value="41" id="minute">41
    <option name="42" value="42" id="minute">42
    <option name="43" value="43" id="minute">43
    <option name="44" value="44" id="minute">44
    <option name="45" value="45" id="minute">45
    <option name="46" value="46" id="minute">46
    <option name="47" value="47" id="minute">47
    <option name="48" value="48" id="minute">48
    <option name="49" value="49" id="minute">49
    <option name="50" value="50" id="minute">50
    
    <option name="51" value="51" id="minute">51
    <option name="52" value="52" id="minute">52
    <option name="53" value="53" id="minute">53
    <option name="54" value="54" id="minute">54
    <option name="55" value="55" id="minute">55
    <option name="56" value="56" id="minute">56
    <option name="57" value="57" id="minute">57
    <option name="58" value="58" id="minute">58
    <option name="59" value="59" id="minute">59
    </select>
    
    <select>
    <option name="AM" value="am" id="am_pm">AM
    <option name="PM" value="pm" id="am_pm">PM
    </select>
    <br>
    
    Enter the path for the sound file you wish to use<br>
    <input type="text" id="path" style="width:300px;"><br>
    <input type="button" id="Search" value="Search"><br>
    
    <input type="reset" value="Reset" onclick="javascript: window.location.reload();">
    <input type="button" value="Exit" onclick="javascript: window.close();">
    <input type="button" value="Run Alarm" onclick="run();">
    </center>
    </table>
    </body>
    </html>
  • risk32
    New Member
    • Mar 2008
    • 98

    #2
    Never mind... I found the documentation on how to fix it... But I have a new problem. Does anyone know how to open a Windows directory menu like you can with websites that have upload buttons? Or where I can find documentation on that?

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Originally posted by risk32
      Never mind... I found the documentation on how to fix it... But I have a new problem. Does anyone know how to open a Windows directory menu like you can with websites that have upload buttons? Or where I can find documentation on that?
      Check out the HTML 'input' element.

      Code:
      <input type="file" />

      Comment

      • risk32
        New Member
        • Mar 2008
        • 98

        #4
        Thanks Markus. It worked perfectly.

        Comment

        • risk32
          New Member
          • Mar 2008
          • 98

          #5
          With the same project going... need some more help. I found this article in http://www.geekpedia.com/tutorial152...-web-page.html. It helps with embedding WMP into the website, but I haven't found anything for changing the source with JS. It's nice for having a static file to play, but what if I want to open one file that's on my desktop and another in 'My Music'? With the <input type="file /> option, the file path is displayed right into the text box... Now I just need to figure out how to copy that into the src="" portion of the embedded object.

          Comment

          • risk32
            New Member
            • Mar 2008
            • 98

            #6
            Here's something that I found along the way... Could this work for Windows Media Player? http://developer.apple.com/internet/ieembedprep.html

            I'm going to have to keep reading this because I have no idea where to start... I look at the code then read what it's supposed to do...but I'm lost

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Unless the page is local, the file on the local file system can't be accessed by JavaScript for use in a media player. Put files on the server if you want to play them from your web page. Then for sound control with JavaScript/Flash, I would recommend SoundManager2

              Comment

              • risk32
                New Member
                • Mar 2008
                • 98

                #8
                Yes, the sound file(s) are local. I'm trying to make it run like an application you would install to your local machine. As far as Sound Manager 2, I couldn't access the link due to restrictions on our web server, but I will look for another link that might work.

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  As I mentioned, JavaScript can't normally access the local file system. Is this application for use on the web or is it going to be run only on your machine?

                  Comment

                  • risk32
                    New Member
                    • Mar 2008
                    • 98

                    #10
                    More for local use, however, I can store it on a web server if need be. I've done the same program with VB, but the only "VB" type program available on these computers is MS Access and Excel. Just trying to do the same with JS considering everyone has access to a web browser.

                    Comment

                    • risk32
                      New Member
                      • Mar 2008
                      • 98

                      #11
                      To clarify what I'm doing, the webpage will be stored on the local machine along with the sound files.

                      Comment

                      • risk32
                        New Member
                        • Mar 2008
                        • 98

                        #12
                        Here's an alarm clock script I stumbled across http://www.javascriptkit.com/script/script2/alarm.shtml, but it's a static URL for the audio file. That's great and all, but I'm trying for something dynamic. I don't exactly want to listen to the same song every morning when I wake up. What I need to do is pass a value from a <input type="file"> box into a variable in JS, then use that variable to supply the URL for a Windows Media Player object stored in another page. I know what needs to happen, but I have no idea how to do it.

                        Comment

                        • risk32
                          New Member
                          • Mar 2008
                          • 98

                          #13
                          Acoder, I found a round-about way to do it, but I'm having a variable compare issue. I'm trying to assign a variable an array value (IE doesn't like this).
                          Code:
                          function run()
                          {
                          
                          var date = new Date(); 
                          var hourNow = date.getHours(); 
                          var minuteNow = date.getMinutes();
                          var secondsNow = date.getSeconds();
                          
                          var weekday=new Array(7);
                          weekday[0]="Sunday";
                          weekday[1]="Monday";
                          weekday[2]="Tuesday";
                          weekday[3]="Wednesday";
                          weekday[4]="Thursday";
                          weekday[5]="Friday";
                          weekday[6]="Saturday";
                          
                          var currentDay = weekday[date.getDay()];
                          
                          var day = document.getElementById("day").value;
                          var hour = document.getElementById("hour").value;
                          var minute = document.getElementById("minute").value;
                          Then, I would compare this variable to the value of the day selected in the form,
                          Code:
                          if (currentDay = day)
                          ...
                          For right now, all I have is an alert for the if / else just to see if it will pass or fail. Is there another way to do this to get the same results? The reason I ask is, I'm trying to set it up to compare the current day / time to what is used in the form. So if I pick Monday, 06:30 AM, it will run on that time. I still need to create a timer that will compare these values every minute to make sure it is correctly ran (within the minute). There is a program available on the web called "College Alarm Clock" that gave me this idea, though I wanted to try to create it in JS / HTML.

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            The comparison should be with double equals:
                            Code:
                            if (currentDay == day)

                            Comment

                            • risk32
                              New Member
                              • Mar 2008
                              • 98

                              #15
                              Originally posted by acoder
                              The comparison should be with double equals:
                              Code:
                              if (currentDay == day)
                              The comparison works, but there's something I must be missing. The alert I set up always returns a fail:

                              Code:
                              var currentDay = weekday[date.getDay()];
                              var day = document.getElementById("day").value;
                              if (currentDay == day)
                               {
                                 alert('Check Passed');
                               }
                              
                              else
                               {
                                 alert('Check Failed');
                               }
                              I replaced the values for the day select in the html with what is displayed through JS. Case-wise they are an exact match. Of course, this is only a test code to see if I'm starting with the right direction. I will end up replacing the alerts with a window.open function to open a webpage to play a song. I ended up creating a secondary webpage that holds the WMP object embedded into it.
                              I turned the file path into a hyperlink with the document.write( ), just needing to create a countdown timer to activate the link once the current time value and the alarm value match.

                              Comment

                              Working...