0

I am using sharepoint 2013 online. I have created a custom list. On this list I have used JSLink to show an icon in a column. I would like to do some logic to show a red or a green icon. I have now in js 2 dates. The date from my column from the current item and the date of today. I would like to do the following check:

var contractEndDate = ctx.CurrentItem.Contract_x0020_einddatum;

    var today = new Date();


    if((contractEndDate + 10 days) > today)
    {
        return "<img src='https://myCompany.sharepoint.com/sites/teams/Sales/SiteAssets/green.png'/>";
    }
    else
    {
        return "<img src='https://myCompany.sharepoint.com/sites/teams/Sales/SiteAssets/red.png'/>";
    }

How can I cast the var contractEndDate to a date? And how can I increase it with 10 days?

1

Not sure what the format of contractEndDate is in this case but try:

var contractEndDate = new Date(ctx.CurrentItem.Contract_x0020_einddatum);
var compareDate = contractEndDate.getDate() + 10
var today = new Date();

if(compareDate > today)
{
    return "<img src='https://myCompany.sharepoint.com/sites/teams/Sales/SiteAssets/green.png'/>";
}
else
{
    return "<img src='https://myCompany.sharepoint.com/sites/teams/Sales/SiteAssets/red.png'/>";
}

There is a risk that new Date(ctx.CurrentItem.Contract_x0020_einddatum) will throw an error... in which case you need to use a supporting method like this one:

function convertSPDate(d) {
/*
*   A function to convert a standard SharePoint
*   date/time field (YYYY-MM-DD HH:MM:SS) to a
*   javascript Date() object
*
*   Author: Ben Tedder (www.bentedder.com)
*/
    // split apart the date and time
    var xDate = d.split(" ")[0];
    var xTime = d.split(" ")[1];

    // split apart the hour, minute, & second
    var xTimeParts = xTime.split(":");
    var xHour = xTimeParts[0];
    var xMin = xTimeParts[1];
    var xSec = xTimeParts[2];

    // split apart the year, month, & day
    var xDateParts = xDate.split("-");
    var xYear = xDateParts[0];
    var xMonth = xDateParts[1];
    var xDay = xDateParts[2];

    var dDate = new Date(xYear, xMonth - 1, xDay, xHour, xMin, xSec);
    return dDate;
}

found here but slightly modified (since month is zerobased in javascript Date())

  • I have try first to return the variable contractEndDate to check if it is filled and give me this error: Invalid Date I think it is not possible to cast this sharepoint value to date. I see some examples on internet where they are split the string in day, month and year. This is realy ugly. I hope there is a better solution. – Ola Mar 4 '14 at 12:29
  • That might be the way you need to go – Robert Lindgren Mar 4 '14 at 12:30
  • Thanks, we are almost there. Why contractEndDate.getDate() returns 4 ? 4 is the day of the date object. – Ola Mar 4 '14 at 13:35
  • Yepp, Date == dayOfMonth – Robert Lindgren Mar 4 '14 at 13:38
  • Thanks it works! only one fix: var dDate = new Date(xYear, xMonth - 1, xDay, xHour, xMin, xSec); – Ola Mar 4 '14 at 14:30
0

Assuming contractEndDate is a DateTime field type:

var today = new Date();
var contractExpiredDate = new Date(contractEndDate); // create Date object from date string
contractExpiredDate.setDate(contractEndDate.getDate() + 10);

if (contractExpiredDate > today) {
   //The remaining code is committed for clarity 
}
  • this is not working: var contractExpiredDate = new Date(contractEndDate); – Ola Mar 5 '14 at 8:39
  • hmm.. i've tested it and it works for me, could you please share some details about error.. – Vadim Gremyachev Mar 5 '14 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.