0

What I am trying is What I am trying to do is

Input Date : eg 5/8/2016 (2 months back from today's date) Current Date : eg 5/10/2016

I want to save the date difference between these 2 dates (Input Date - Current Date)

Later I want to Add this difference in dates to Current Date , and display the Input Date as it is.

Input Date can be any date behind from current date or future date

I am trying to use moment.Duration but it is giving me wrong output dates (always difference of 2/3 days added to current date)

Implementing functionality of relative date

Current date eg : 30/9/2016 Input date eg : 30/1/2016

I want to save the Difference between these 2 dates in DB (in ISO 8601 format duration UTC Value "-P8M4DT5.61S")

var actualDate = moment("30/1/2016");
var nowUtc = moment().utc();
var diff = actualDate.diff(nowUtc);
var duration = moment.duration(diff);
var savedDate= JSON.stringify(duration);

Here diff value which I am getting is wrong. Is there any issue in moment.js ?

later I parse the saved date and then try to add the Diff to current date which results is wrong calculation

var diffValue = JSON.parse(savedDate);
var durationVal = moment.duration(diffValue);
var nowUtc = moment.utc();
var originalDate = nowUtc.add(durationVal);

This 'originalDate ' I am getting should be 30/1/2016 but it always comes wrong

Can any one Help here ?

How to Save Date Difference In DB ? (in ISO 8601 format duration UTC Value) Then using the difference value retrieve date

sujitgr8
  • 73
  • 6
  • 3
    Just a guess, but should you be converting _both_ dates to UTC before comparing them (in the first snippet)? – ADyson Sep 30 '16 at 14:18
  • 1
    What are you getting and what are you expecting? Please see [mcve]. – Heretic Monkey Sep 30 '16 at 14:26
  • See if this helps. http://stackoverflow.com/questions/18623783/get-the-time-difference-between-two-datetimes – s7vr Sep 30 '16 at 16:12
  • @ADyson I was getting wrong date using 'diff' hence I tried converting to utc , even I dont convert it to utc I never get proper date difference – sujitgr8 Oct 03 '16 at 06:52
  • "never get a proper date difference". What do you mean exactly? What inputs did you give, what outputs did you expect, and what outputs did you actually get? Have you read the docs to understand what output to expect? http://momentjs.com/docs/#/displaying/difference/ – ADyson Oct 03 '16 at 09:31

0 Answers0