Forum Discussion

Cindy Zalme's avatar
Cindy Zalme
Copper Contributor
Oct 19, 2017

SharePoint Online Date Calculation

I'd like to calculate the number of days between [today] and the due date in a SharePoint Online task list.  Not sure why this seems so difficult or why I can't readily find an answer online.  Any ideas?  Thanks so much ahead of time for your help. 

  • If you are using the New experience interface than you can achieve it with JSON column formatting.

     

    Here is the example: 

     

    {
    "$schema": "http://columnformatting.sharepointpnp.com/columnFormattingSchema.json",
    "elmType": "div",
    "txtContent": {
    "operator": "/",
    "operands": [
    {
    "operator": "-",
    "operands": [
    {
    "operator": "Number()",
    "operands": [
    "@now"
    ]
    },
    {
    "operator": "Number()",
    "operands": [
    "[$DueDate]"
    ]
    }
    ]
    },
    86400000
    ]
    }
    }

     

    You can see in the example that it converts "@now" and "[$DueDate]" into numbers, then subtract them. Finally, the subtraction result is divided by 86400000 - the number of milliseconds in a day.

     

    Cheers

     

    • CindyZ's avatar
      CindyZ
      Iron Contributor

      Thanks very much for this and especially your explanation.  I did some quick searching but was not able to quickly ascertain why it is returning NAN even though my column is formatted as a number.  

    • Abbyamante's avatar
      Abbyamante
      Copper Contributor

      Pavel Sheludkov thank you for sharing this format. this works on my sharepoint list column. however, do you know how to remove the decimal points and just make it a round number of days?

       

       

      • asifkhawaja7's avatar
        asifkhawaja7
        Copper Contributor

        Abbyamante , just use the floor() function to round the number down. 

         

        "=floor((Number([$DueDate])-Number(@now))/86400000)"

  • Although Pavel Sheludkov already provided an answer, I just wanted to mention that view and column formatting now accept formulas that significantly shorten the expression.

     

    In this case you would just write:

    "=(Number([$DueDate])-Number(@now))/86400000"

    • CindyZ's avatar
      CindyZ
      Iron Contributor

      Oh my gosh, Christophe!  Thanks so much for your help and answer!  Great to hear from you again.  You helped me on some  projects over three years ago!  

    • Visuary's avatar
      Visuary
      Copper Contributor

      Christophe Humbert is there any was to get the correct integer value of that 15 decimal number that is returned? 


      Christophe Humbert wrote:

      In this case you would just write:

      "=(Number([$DueDate])-Number(@now))/86400000"




      • AmandaR's avatar
        AmandaR
        Copper Contributor

        Visuary Did you ever figure out how to display the value as an interger?

  • Matt Coats's avatar
    Matt Coats
    Steel Contributor

    The problem is SharePoint Online treats that as a calculation, and calculations are only refreshed when the item is modified in any way (at least, out of the box). The only way I could find to force the date to recalculate was to create a loop in SharePoint Designer or Flow to overwrite something in an item with its current value until due date = today, which technically changes nothing about the item while still counting as a modification, resulting in a refresh of a date difference calculation. It's an expensive workaround, especially on large lists with lots of due date items, but it's the only non-custom way I know of to manage this.

    • CindyZ's avatar
      CindyZ
      Iron Contributor

      Thanks, Matt...may not be beautiful, but that will work!  

  • What are you trying to accomplish with the calculation? That I would like more to know :)
    Kind regards, John
    • Cindy Zalme's avatar
      Cindy Zalme
      Copper Contributor

      "How many days left until this item is due?"  For now, I'm looking for all days since elimininating weekends may be a challenge for now.   I have a start date and a due date.  How many days between those two?  Thanks!  

Share

Resources