It is almost that time of year again where we get some time off work and eat until we nearly explode and watch whatever James Bond film happens to be on the TV. This month we are going to create a PaperCut Print Script to tell the users how many days there are to go until the big day, So grab a cup of coffee or tea and let's begin.

We begin our script this month by creating a new Date object this will make it easier to work with dates which is usually done by converting the date to a number and working from there, This is called <a href="https://en.wikipedia.org/wiki/Unix_time">Unix Time</a> or POSIX / Epoch Time.
<pre>var objDate = new Date();</pre>
Next up is to set the date we want to use for our countdown and convert it to Unix Time using the Date.parse() method and also set a variable to today's date that we will use later.
<pre>xmas = Date.parse( "Dec 25, " + objDate.getFullYear() );
today = Date.parse( objDate );
</pre>
So now we need to do a little bit of math to work out what difference is between today and December 25th then convert it to a number that will actually mean something to a user and also round it to the nearest whole number.
<pre>daysToGo = Math.round( ( xmas - today ) / ( 1000 * 60 * 60 * 24 ) );
</pre>
All we need to do now is add in a few if statements to handle the three possible outcomes, then send the user a message that is relevant. We only have three possible cases to take into consideration here; Christmas has not yet happened, Christmas has happened and today is Christmas (We don't recommend working on Christmas day unless something important is on fire).
<pre>if ( daysToGo == 0 ) {
    actions.client.sendMessage( "Merry Christmas!" );
    return;
}
if ( daysToGo &lt; 0 ) {
    actions.client.sendMessage( "We hope you had a good Christmas" );
    return;
}
if ( daysToGo &gt; 0 ) { 
    actions.client.sendMessage( "Christmas is only " + daysToGo + " days away" );
    return;
}
</pre>
That is it now you have a script you can use to let your users know how many days there are until Christmas, You could restrict this to certain groups of users or even use it to remind people of any events you have coming up.

As always the complete print script is below and if you have your own requirements for a print script and you don't have the time or in-house experience to make it you can find out how we can help you by sending an email to our sales team at <a href="mailto:sales@selectec.com">sales@selectec.com</a>
<pre>/*
 * Days until Christmas
 *
 * Show the users how many days there are until Christmas
 *
 * Read more at https://staging.selectec.com/script-of-the-month-2-days-until-christmas
 */
function printJobHook( inputs, actions ) {
    if ( !inputs.job.isAnalysisComplete ) {
        // No job details yet so return.
        return;
    }

    var objDate = new Date(); // New date object
    xmas = Date.parse( "Dec 25, " + objDate.getFullYear() ); // This is the date we want
    today = Date.parse( objDate ); // What is todays date again?
    daysToGo = Math.round( ( xmas - today ) / ( 1000 * 60 * 60 * 24 ) ); // Little bit of math

    if ( daysToGo == 0 ) {
        // It must be xmas today
        actions.client.sendMessage( "Merry Christmas!" );
        return;
    }

    if ( daysToGo &lt; 0 ) {
        // Xmas has gone
        actions.client.sendMessage( "We hope you had a good Christmas" );
        return;
    }

    if ( daysToGo &gt; 0 ) {
        // It is almost xmas time
        actions.client.sendMessage( "Christmas is only " + daysToGo + " days away" );
        return;
    }
 }
</pre>