Antonio Vasilev
Antonio Vasilev

Reputation: 2965

Need explanation of this Date Processing function

Could anyone please explain the below code to me?

For example, i would like to set Today's date to today (21st of November, 2012) and the end date to the 3rd of December.

The reason for this is because i want to loop through a list of items, determine whether they are in the "past", "present" or "future" and assign a class to them accordingly.

I hope this makes sense! Any help is greatly appreciated and much welcomed!

function daysTilDate(expiredate){

    expiredate ="12/"+expiredate+"/2012";

    var thisDay=new Date(expiredate);

    var CurrentDate = new Date();

    var thisYear=CurrentDate.getFullYear();

    thisDay.getFullYear(thisYear);

    var DayCount=(thisDay-CurrentDate)/(1000*60*60*24);

    DayCount=Math.round(DayCount);

    return DayCount;
}

Upvotes: 1

Views: 176

Answers (3)

Ed Bayiates
Ed Bayiates

Reputation: 11230

I am not going to review the code, but I can answer your question of "I want to loop through a list of items, determine whether they are in the past, present, or future".

First, you want to construct your target date. If it's "now", just use new Date(). If it's a specific date, use new Date(dateString).

Second, Date objects in JavaScript have various members that return the date's characteristics. You can use this to compare dates. So, let's say you have your date strings in an array:

function loopDates(targetDateString, myDates) {
    var targetDate, nextDate, status, ix;
    targetDate = new Date(targetDateString);
    for (ix = 0;  ix < myDates.length;  ++ix) {
        nextDate = new Date(myDates[ix]);
        if (nextDate.getFullYear() < targetDate.getFullYear()) {
             status = "past";
        } else if (nextDate.getFullYear() > targetDate.getFullYear()) {
             status = "future";
        } else {
            // Year matches, compare month
            if (nextDate.getMonth() < targetDate.getMonth()) {
                status = "past";
            } else if (nextDate.getMonth() > targetDate.getMonth()) {
                status = "future";
            } else {
                // Month matches, compare day of month
                if (nextDate.getDate() < targetDate.getDate()) {
                    status = "past";
                } else if (nextDate.getDate() > targetDate.getDate()) {
                    status = "future";
                } else {
                    // Day matches, present
                    status = "present";
                }
            }
        }
        console.log("Date " + myDates[ix] + " is " + status + " from " + targetDateString);
    }
}

loopDates("11/17/2012", ["11/16/2012", "11/17/2012", "11/18/2012"]);

This will log:

Date 11/16/2012 is past from 11/17/2012
Date 11/17/2012 is present from 11/17/2012
Date 11/18/2012 is future from 11/17/2012

Working jsFiddle here.

If you want to work with a comprehensive Date class, use DateJS, an open source JavaScript date and time processing library with some impressive features.

Upvotes: 1

Serkan Inci
Serkan Inci

Reputation: 284

You can simplify the method like below if you want to calculate the days to an expire date. Please note that if you don't specify a test date, it'll take the current date as the test date.

​function ​daysTilData(expireDate, testDate) {

    if(typeof testDate === "undefined"){
        testDate = new Date(); // now        
    }

    var diff = expireDate - testDate;

    // minus value meaning expired days
    return Math.round(diff/(1000*60*60*24));
}

alert(daysTilData(new Date("12/31/2012")));
// result 40

alert(daysTilData(new Date("12/31/2012"), new Date("1/12/2013")));
// result -12

Upvotes: 2

Jim
Jim

Reputation: 6881

Here's a line by line explanation.

The function declaration...

function daysTilDate(expiredate){

Takes the parameter expiredate sets it equal to the same value with "12/" prepended and "/2012" appended. so if the value of expiredate was "10", the new value is now "12/10/2012"...

expiredate ="12/"+expiredate+"/2012";

Instantiates a new Date object named thisDay using the expiredate string...

var thisDay=new Date(expiredate);

Instantiates a new Date object named CurrentDate, using the default constructor which will set the value equal to today's date...

var CurrentDate = new Date();

Gets just the Year segment from CurrentDate (which was earlier set to today's date)...

var thisYear=CurrentDate.getFullYear();

Gets the Year segment from thisDay (which was earlier set to "2012")...

thisDay.getFullYear(thisYear);

Gets the difference between thisDay and CurrentDate, which is in milliseconds, and multiplies that by 1000*60*60*24 to get the difference in days...

var DayCount=(thisDay-CurrentDate)/(1000*60*60*24);

Rounds the previously calculated difference...

DayCount=Math.round(DayCount);

Returns the difference between today and the passed-in day in December 2012...

return DayCount;

}

Note that the 2 lines that get the year segments are extraneous, because those values are never used...

Upvotes: 1

Related Questions