www.webdeveloper.com
Results 1 to 8 of 8

Thread: Appending leading zeros in a script

  1. #1
    Join Date
    Apr 2008
    Posts
    9

    Smile Appending leading zeros in a script

    Hi, all:

    I'm trying to edit a script to give me a customized tab in Firefox. I want the tab to automatically open a specific Google search of tv.com to give me only the shows that will be on today. Here's what I've got:

    Code:
    javascript:var d=new Date();location.href='http://www.google.com/search?hl=en&q=site%3Awww.tv.com\+"Airs%3A+'+[d.getMonth()+1]+'/'+(d.getDate())+'/'+[d.getYear()-100]+'"';
    Here's the result it gives me in Google (yes I know it's hacky, but I'm not particularly skilled in JS, and this is just for fun anyways):

    Your search - site:www.tv.com "Airs: 5/13/9" - did not match any documents.

    I need the date search to append leading zeros if the month, day, or year is a single digit; I've seen multiple ways to append leading zeros, but I don't really know how to integrate them into this script to make it work. Does anyone have any suggestions?

    Thanks!

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

    Lightbulb Consider this ...

    Can you use this:
    Code:
    <html>
    <head>
    <title>Format Date</title>
    <script type="text/javascript">
    function ConvertDate(info) {
      var tmp = info.split('/');
      for (var i=0; i<tmp.length; i++) {
        if (tmp[i] < 10) { tmp[i] = '0'+tmp[i]; }
      }
      return tmp.join('/');
    }
    </script>
    </head>
    <body>
    <input id='dtfrom' type="text" value="5/1/9">
    <button
     onClick="document.getElementById('dtto').value=ConvertDate(document.getElementById('dtfrom').value)">
     Converts to </button
    <input id='dtto' type="text" value="">
    </body>
    </html>

  3. #3
    Join Date
    Apr 2008
    Posts
    9
    Quote Originally Posted by JMRKER View Post
    Can you use this:
    Code:
    <html>
    <head>
    <title>Format Date</title>
    <script type="text/javascript">
    function ConvertDate(info) {
      var tmp = info.split('/');
      for (var i=0; i<tmp.length; i++) {
        if (tmp[i] < 10) { tmp[i] = '0'+tmp[i]; }
      }
      return tmp.join('/');
    }
    </script>
    </head>
    <body>
    <input id='dtfrom' type="text" value="5/1/9">
    <button
     onClick="document.getElementById('dtto').value=ConvertDate(document.getElementById('dtfrom').value)">
     Converts to </button
    <input id='dtto' type="text" value="">
    </body>
    </html>
    Do I simply paste this whole block into the URL bar just as I do with my script?

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

    Arrow

    Quote Originally Posted by tarahmarie View Post
    Do I simply paste this whole block into the URL bar just as I do with my script?
    No, just really only need the function.
    Pass the 'info' to it from where ever your date is supplied.

    Note, the function could be modified further if you always get a "new Date()"
    instead of reading from a text box as in the example. The example allows you
    to try different inputs that you might receive to test for correct actions.

    How you use the function depends upon your needs.
    Your example code doesn't supply enough information for me to advise.
    In your example, you can only supply the current date so I not sure how
    you are going to specify different dates and air times.

  5. #5
    Join Date
    Apr 2008
    Posts
    9
    The idea is specifically to get the current date. My problem is that I'm not a JS coder...I write Java, HTML, CSS, etc, etc. All I want is this one script, because I want to be able to search the tv.com site on any given day specifically for the new tv shows.

    You said to put the function into the script; when I do so (and I'm not at all sure I'm doing it correctly), the script doesn't work when I paste it into the URL bar. Here's what I'm pasting in:

    Code:
    javascript:var d=new Date();location.href='http://www.google.com/search?hl=en&q=site%3Awww.tv.com\+"Airs%3A+'+[d.getMonth()+1]+'/'+(d.getDate())+'/'+[d.getYear()-100]+'function ConvertDate(info) {  var tmp = info.split('/'); for (var i=0; i<tmp.length; i++) {if (tmp[i] < 10) { tmp[i] = '0'+tmp[i]; }} return tmp.join('/');}"';
    Is this the function you were talking about?

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,355

    Lightbulb Try this ...

    Simplified version:
    Code:
    <html>
    <head>
    <title>Format Date</title>
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/showthread.php?p=1005807#post1005807
    
    function ConvertDate() {
      var str = '';
      var dt = new Date();
      var mm = dt.getMonth()+1;  if (mm < 10) { mm = '0'+mm; }
      var dd = dt.getDate();  if (dd < 10) { dd = '0'+dd; }
      var yy = ''+dt.getFullYear();  
          yy = yy.substr(2,2);
      var str = mm+'/'+dd+'/'+yy;
      return str;
    }
    function InitTV() {
      document.getElementById('anchorID').href
       =  'http://www.google.com/search?h1+en&q=site%3Awww.tv.com\+"Airs%3A+'+ConvertDate()+'"';
      document.getElementById('spanID').innerHTML=' "Airs: '+ConvertDate()+'"';
    }
    </script>
    </head>
    Site information<p>
    <body onload="InitTV()">
    <a href="#" id="anchorID">www.tv.com</a><span id="spanID"></span>
    <p>More site information
    </body>
    </html>

  7. #7
    Join Date
    Apr 2008
    Posts
    9
    Quote Originally Posted by JMRKER View Post
    Simplified version:
    Code:
    <html>
    <head>
    <title>Format Date</title>
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/showthread.php?p=1005807#post1005807
    
    function ConvertDate() {
      var str = '';
      var dt = new Date();
      var mm = dt.getMonth()+1;  if (mm < 10) { mm = '0'+mm; }
      var dd = dt.getDate();  if (dd < 10) { dd = '0'+dd; }
      var yy = ''+dt.getFullYear();  
          yy = yy.substr(2,2);
      var str = mm+'/'+dd+'/'+yy;
      return str;
    }
    function InitTV() {
      document.getElementById('anchorID').href
       =  'http://www.google.com/search?h1+en&q=site%3Awww.tv.com\+"Airs%3A+'+ConvertDate()+'"';
      document.getElementById('spanID').innerHTML=' "Airs: '+ConvertDate()+'"';
    }
    </script>
    </head>
    Site information<p>
    <body onload="InitTV()">
    <a href="#" id="anchorID">www.tv.com</a><span id="spanID"></span>
    <p>More site information
    </body>
    </html>
    Ok, I tried that, and here's the result I get after pasting it in to the URL bar:

    Your search - <html><head><title>Format Date</title><script type="text/javascript">// From: http://www.webdeveloper.com/forum/sh...005807function ConvertDate() {var str = '';var dt = new Date();var mm = dt.getMonth()+1; if (mm < 10) { mm = '0'+mm; }var dd = dt.getDate(); if (dd < 10) { dd = '0'+dd; }var yy = ''+dt.getFullYear();yy = yy.substr(2,2);var str = mm+'/'+dd+'/'+yy;return str;}function InitTV() {document.getElementById('anchorID').href= 'http://www.google.com/search?h1+en&q=site%3Awww.tv.com\+"Airs%3A+'+ConvertDate()+'"';document.getElementById('spanID').inn erHTML=' "Airs: '+ConvertDate()+'"';}</script></head>Site information<p><body onload="InitTV()"><a href="#" id="anchorID">www.tv.com</a><span id="spanID"></span><p>More site information</body></html> - did not match any documents.

    At least it's now being recognized as javascript!

  8. #8
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,355
    Are you trying to create a bookmarklet?

    If yes, none of the solutions I have provided have been designed with that in mind.
    And, if so, your original request is not very clear on that point.

    I thought you have been trying to create a website with the link to the TV search
    being updated every day that you enter into the site. Save the last script file
    and execute it with your browser pointing to the saved file. It works fine then.

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