Solved

Javascript datediff function needed

Posted on 2007-06-03
4
4,122 Views
Last Modified: 2010-08-05
Hi everyone,

I need a javascript function to calculate the number of days between two dates in YYYYMMDD format.  Anyone have anything like this handy?

eg days = daysDiff('20070301','20061215');

Thanks!
0
Comment
Question by:quiTech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 19205145
function daysDiff(day1, day2) {
    var diff = Math.abs(Date.parse(day1) - Date.parse(day2));
    var daysDiff = Math.floor(diff / 1000 / 60 / 60 / 24);
    return daysDiff;
}

alert(daysDiff('2007/03/01', '2006/12/15'));
0
 

Author Comment

by:quiTech
ID: 19205222
Hi Zyloch,

That looks pretty good.  How would this work with a date that doesn't have the slashes (ie 20070301 instead of 2007/03/01).  Do I have to add the slashes or should it work without?  If I have to add them, how do I do that? (sorry my javascript skills are lacking severly ;-)

Thanks!
0
 
LVL 36

Accepted Solution

by:
Zyloch earned 500 total points
ID: 19205261
Try this:

function daysDiff(day1, day2) {
    day1 = addSlashes(day1);
    day2 = addSlashes(day2);
    var diff = Math.abs(Date.parse(day1) - Date.parse(day2));
    var daysDiff = Math.floor(diff / 1000 / 60 / 60 / 24);
    return daysDiff;
}

function addSlashes(day) {
    day = day.substring(0,4) + '/' + day.substr(4,2) + '/' + day.substr(6, 2);
    return day;
}

alert(daysDiff('20070301', '20061215'));
0
 

Author Comment

by:quiTech
ID: 19209533
WOW works perfectly!  Thanks so much!!!
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question