Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-27-2013, 02:51 AM   PM User | #1
JonBMN
New Coder

 
Join Date: Feb 2013
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
JonBMN is an unknown quantity at this point
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..
JonBMN is offline   Reply With Quote
Old 02-27-2013, 02:55 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 02-27-2013, 02:58 AM   PM User | #3
JonBMN
New Coder

 
Join Date: Feb 2013
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
JonBMN is an unknown quantity at this point
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?
JonBMN is offline   Reply With Quote
Old 02-27-2013, 03:00 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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);
__________________
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.

Last edited by Old Pedant; 02-27-2013 at 03:03 AM..
Old Pedant is offline   Reply With Quote
Old 02-27-2013, 03:02 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 02-27-2013, 03:09 AM   PM User | #6
JonBMN
New Coder

 
Join Date: Feb 2013
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
JonBMN is an unknown quantity at this point
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!
JonBMN is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:15 AM.


Advertisement
Log in to turn off these ads.