Calculate days passed since event

Comments

7 comments

  • Niclas König

    Never mind! Fixed with incredibly simple js:
    moment(Date of experiment).fromNow();

    0
    Comment actions Permalink
  • Philip Hawkshaw

    Niclas

    Can you post more comprehensive details of what you're doing please?

    I would like to wrap this days since event in an if statement.

    0
    Comment actions Permalink
  • Niclas König

    Philip, as a part of a logging app for a study, there is a date field simply called "Date of experiment". Following this, I've entered the formula according to my first reply: moment(Date of experiment).fromNow();
    (Addition: if you add "true" in the last parentheses -- moment(Date of experiment).fromNow(true); -- the output changes from for instance "7 days ago" to just "7 days")
    All this is just to keep track of how much time has elapsed since the experiment!

    0
    Comment actions Permalink
  • John Taylor

    How can we get this date calculation to display days instead of months?

    0
    Comment actions Permalink
  • Niclas König

    John, apparently by writing

    var now = moment(Date of experiment)
    var then = moment(Date of termination)
    then.diff(now, 'days')

    I know I've seen a way to also customize entirely the output, so that you could for example get something similar to:
    "years: 0
    months:1
    days:3
    hours:...."
    and so on.

    0
    Comment actions Permalink
  • John Taylor

    Most of the date requirements I have don't have termination dates yet. I was seeking to check them again today's date, i.e., how many days since this was sent out. It looks like the javascript you included requires a termination date, so it won't work for me. I can't figure out how to have today's date replace the termination date in your script because it's never defined correctly no mater what I put in. Anyone have any ideas?

    This is my script as it exists from this thread....

    moment(Start date).fromNow();

    0
    Comment actions Permalink
  • Niclas König

    Maybe the documentation over at http://momentjs.com/ is helpful? Also, just to be clear, are you referring to the date field (by placing @ in front of the name)?

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk