Results 1 to 9 of 9

Thread: javascript trouble with varibles (date)

  1. #1
    Join Date
    Mar 2011
    Posts
    1,914
    Thanks
    59
    Thanked 105 Times in 103 Posts
    Blog Entries
    4

    Default javascript trouble with varibles (date)

    Hello everybody,
    I have this code
    HTML Code:
    // Date/Time
    currentDate = new Date()
    day = currentDate.getDate()
    
    if (day < "10") {
    zero = "0"
    day = (  Troublesom Spot);
    }
    Where troublesom spot is, I would like to have 0 and then the day variable so that if the date is less than ten, it would display like this.

    Instead of 5/2/2012
    05/2/2012


    I'm sorry if I haven't been clear enough,
    Keyboard1333
    Last edited by keyboard; 02-07-2012 at 01:17 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    if (day <  10) {
    	day = '0' + day;
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2011
    Posts
    1,914
    Thanks
    59
    Thanked 105 Times in 103 Posts
    Blog Entries
    4

    Default

    I'm still getting the same problem. Here's my full code.

    index.html
    HTML Code:
    <html>
    <head>
    	<script language="JavaScript" src="function.js"></script>
    	<script language="JavaScript" src="settings.js"></script>
    </head>
    <body>
    <script language="JavaScript">
      	alert(fldrdate);
    </script>
    <br />
    <script language="JavaScript">
    </script>
    </body>
    </html>
    function.js
    HTML Code:
    // Date/Time
    currentDate = new Date()
    day = currentDate.getDate()
    if (day <  10) {
    	day = '0' + day;
    }
    month = currentDate.getMonth() + 1
    year = currentDate.getFullYear()
    currentTime = new Date()
    hours = currentTime.getHours()
    minutes = currentTime.getMinutes()
    suffix = "AM";
    if (hours >= 12) {
    suffix = "PM";
    hours = hours - 12;
    }
    if (hours == 0) {
    hours = 12;
    }
    if (minutes < 10)
    minutes = "0" + minutes
    
    
    fldrtime =  (+ hours + ":" + minutes + " " + suffix + "")
    fldrdate =  (+ day + "/" + month + "/" + year + "")
    
    Settings.js is currently empty.

    Is it something to do with the alert -
    HTML Code:
    <script language="JavaScript">
      	alert(fldrdate);
    </script>
    Last edited by keyboard; 02-05-2012 at 06:31 AM. Reason: Cause I'm special

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    No, it's here:

    Code:
    fldrdate =  (+ day + "/" + month + "/" + year + "")
    Because that red + isn't concatenating day to a preceding string, it's signing the day value. Signing a string turns it into a number*. Numbers preceded with 0's are stripped of those zeros. It's the same as doing:

    Code:
    fldrdate =  ((1 * day) + "/" + month + "/" + year + "")
    You can do:

    Code:
    fldrdate =  ("" + day + "/" + month + "/" + year + "")
    But it's more common to simply:

    Code:
    fldrdate =  (day + "/" + month + "/" + year + "")
    Or (parentheses are not required here and adding an empty string at the end of what is already a string does nothing):

    Code:
    fldrdate =  day + "/" + month + "/" + year;
    BTW - You've got a lot of undeclared variables in the code. It's not a problem in this particular case. But as your code gets more complex, and/or as it's used on pages with more complex markup, sooner or later that practice will become the source of one or more errors and/or unexpected results.



    *If it can make sense as number it's converted to the most concise expression of that number, otherwise it becomes NaN which technically, even though it literally means 'Not a Number', is still a number:

    Code:
    alert(typeof NaN); //alerts 'number'
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Mar 2011
    Posts
    1,914
    Thanks
    59
    Thanked 105 Times in 103 Posts
    Blog Entries
    4

    Default

    Thanks heaps jscheuer1


    Quote Originally Posted by jscheuer1 View Post
    No, it's here:
    BTW - You've got a lot of undeclared variables in the code. It's not a problem in this particular case. But as your code gets more complex, and/or as it's used on pages with more complex markup, sooner or later that practice will become the source of one or more errors and/or unexpected results.
    So when it's outside a
    Edit: function
    i should always use

    Code:
    var variable = "test"
    instead of

    Code:
    variable = "test"
    Last edited by keyboard; 02-06-2012 at 10:20 PM.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I don't know what you mean by "when it's outside a variable"

    You seem to like to use variables in the global scope. These are variables available to all functions. The first time you declare a variable like that you should use the var keyword:

    Code:
    var myvariable = 'whatever';
    At least that way it won't conflict with tags on the page that have an id of the same name. This only happens in IE where document.all is assumed if a variable isn't formally declared with the var keyword. But when it does happen, it can often be very hard to figure out and fix.

    A better overall practice is to use no global variables or only one. Code can be constructed that way and it makes conflicts with other scripts virtually impossible.

    Here's the same code with only one global variable (mydatedata):

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    // Date/Time
    var mydatedata;
    (function(){
    	var currentDate = new Date(),
    	day = currentDate.getDate(),
    	month = currentDate.getMonth() + 1,
    	year = currentDate.getFullYear(),
    	currentTime = new Date(),
    	hours = currentTime.getHours(),
    	minutes = currentTime.getMinutes(),
    	suffix = "AM";
    	if (day <  10) {
    		day = '0' + day;
    	}
    	if (hours >= 12) {
    		suffix = "PM";
    		hours = hours - 12;
    	}
    	if (hours == 0) {
    		hours = 12;
    	}
    	if (minutes < 10){
    		minutes = "0" + minutes
    	}
    
    	mydatedata = {
    		fldrtime: hours + ":" + minutes + " " + suffix,
    		fldrdate: day + "/" + month + "/" + year
    	};
    })();
    </script>
    </head>
    <body>
    <script type="text/javascript">
      	alert(mydatedata.fldrdate);
    </script>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Mar 2011
    Posts
    1,914
    Thanks
    59
    Thanked 105 Times in 103 Posts
    Blog Entries
    4

    Default

    Thanks for your reply!
    Oops, I meant to write function rather than variable.

    HTML Code:
                   day = currentDate.getDate(),
    	month = currentDate.getMonth() + 1,
    	year = currentDate.getFullYear(),
    	currentTime = new Date(),
    	hours = currentTime.getHours(),
    	minutes = currentTime.getMinutes(),
    	suffix = "AM";
    Wouldn't all these variables still be global variables because there's no var before them?

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Ah, but there is:

    Code:
    	var currentDate = new Date(),
    	day = currentDate.getDate(),
    	month = currentDate.getMonth() + 1,
    	year = currentDate.getFullYear(),
    	currentTime = new Date(),
    	hours = currentTime.getHours(),
    	minutes = currentTime.getMinutes(),
    	suffix = "AM";
    Because of the commas at the ends of the lines there, the variable declaration doesn't end until the semi-colon.

    You can test it out by doing in the body:

    Code:
    <script type="text/javascript">
      	alert(typeof hours);
    </script>
    which gives 'undefined'.


    Oh and your question makes perfect sense if you meant outside a function. In that case, yes - the first time you declare a global you should use the var keyword. It will avoid a possible conflict with the implied document.all collection in IE browsers*. And if you have a conflict with another script, a strict script parser should be able to tell if a variable is declared twice and show you where. But, as I say - those types of conflicts can be avoided by not exposing so many global variables.


    *for instance, with the page from my last post, if you switch the body to:

    Code:
    <body>
    <div id="hours"></div>
    <script type="text/javascript">
      	alert(mydatedata.fldrdate);
    </script>
    <script type="text/javascript">
      	alert(typeof hours);
    </script>
    </body>
    Most if not all browsers will still say 'undefined', except for IE which will say 'object' because it's sees it as meaning:

    Code:
    alert(typeof document.all['hours']);
    And if you do:

    Code:
    <div id="hours"></div>
    <script type="text/javascript">
    	hours = 12;
      	alert(typeof hours);
    </script>
    In IE 8 or less, it will tell you there's an error on the highlighted line - "Object doesn't support this property or method".

    That's a fatal error by the way, the kind that stops all subsequent javascript parsing.

    All others will be fine and say 'number', which IE 8 and less will do as well if you do it like I was saying:

    Code:
    <div id="hours"></div>
    <script type="text/javascript">
    	var hours = 12;
      	alert(typeof hours);
    </script>
    Last edited by jscheuer1; 02-07-2012 at 01:59 AM. Reason: fix typo - add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Mar 2011
    Posts
    1,914
    Thanks
    59
    Thanked 105 Times in 103 Posts
    Blog Entries
    4

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
  •