Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Trying to get the days between dates.

    no regexp please. I feel my logic is sound on this but I'm definitely doing something wrong. Is it a logic error on my part or am I not passing Date.parse with the right value. I'm not sure please help! thank you for your replies!

    Code:
    //days between function only yyyy-mm-dd will work.
    function daysBetween(firstd, secondd, string) {
    	var date1 = firstd,
    		date1 = Date.parse(date1),
    		date2 = secondd,
    		date2 = Date.parse(date2),
    		str = string
    		;
    	if (str === "days") {
    		var day = 24 * 60 * 60 * 1000; //hours times minutes times seconds times milliseconds
    		date1ms = date1.getTime();	//we convert both dates to milliseconds.
    		date2ms = date2.getTime();
    		var thedifference = Math.abs(date1ms - date2ms);
    		return Math.round(thedifference / day);
    	}
    	else if (str === "hours") {
    		var hour = 60 * 60 * 1000;
    		date1ms = date1.getTime();
    		date2ms = date2.getTime();
    		var thedifference = Math.abs(date1ms - date2ms);
    		return Math.round(thedifference / hour);
    	}
    }
    Last edited by JonBMN; 02-27-2013 at 04:05 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,449
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    Date.parse() *ALREADY* is giving you the milliseconds since 1/1/1970.

    It is handing you simply a *NUMBER*.

    It is *NOT* giving you a Date object, so trying to do date.getTime() is giving you an error.

    Time to learn to DEBUG DEBUG DEBUG!

    Any good debugger (I prefer Chrome's, for integration and ease of use) would have shown you what was in date1 and date2.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah! the little things haha; I shall go and do that. I use firebug by the way if theres really any difference between chromes. is there?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,449
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    Why do you persist in copying the function arguments to other variables?

    Why not simply:
    Code:
    function daysBetween(firstd, secondd, str) 
    {
        var date1 = Date.parse(firstd),
            date2 = Date.parse(secondd);
        ...
    And, actually, there's nothing wrong with doing
    Code:
    function daysBetween(date1, date2, str) 
    {
        date1 = Date.parse(date1);
        date2 = Date.parse(date2);
        ...
    And may I point out that you repeated this code:
    Code:
    		date1ms = date1.getTime();	//we convert both dates to milliseconds.
    		date2ms = date2.getTime();
    		var thedifference = Math.abs(date1ms - date2ms);
    for no apparent reason?

    Of course, now you know that you could have done simply
    Code:
    		var thedifference = Math.abs(date1 - date2);
    Last edited by Old Pedant; 02-27-2013 at 03:03 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,449
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    Quote Originally Posted by JonBMN View Post
    Ah! the little things haha; I shall go and do that. I use firebug by the way if theres really any difference between chromes. is there?
    Try them both. Use the one you like better.

    Chrome does a better job of presenting variables, including scope variables, in my opinion. But it's not like either is any slouch.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New Coder
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You are right about all of those; except to me its a little clearer at this stage because if I wanted to I could change those variable names without having to change the arguments, but I am a beginner.

    Pertaining to all your other snippets I actually feel very good because I figured to do those after your first reply which was a great push in the right direction.

    I'll definitely have to try the chrome one for future projects and and compare on how much I like them, but thanks for the suggestion!


  •  

    Posting Permissions

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