Currently, I am tackling a project that necessitates a specific text response related to a date object.
"1 day 7 hours away" --- This format is crucial; alternatives such as "31 hours away" or "1 day away" will not suffice. -- For language switching purposes between English and German, I am utilizing moment js along with the moment.js language locale:
moment.locale('de')
In order to achieve this, I have generated a fabricated date object using moment js.
var futureDate = new Date()
futureDate.setDate(futureDate.getDate() + 1)// add a day
futureDate.setHours(7)// add 7 hours
However, when attempting to display the moment js result,
moment(futureDate).endOf('day').fromNow()
The output simply states "in a day."
How can I adjust the moment function to properly handle 1 day 7 hours, along with potentially restructuring the sentence?
--- Here is an example of code snippet attempt
moment.locale('de') // toggle between en and de -- english and german
var futureDate = new Date()
futureDate.setDate(futureDate.getDate() + 1)// add a day
futureDate.setHours(7)// add 4 hours
// Provides results in hours
console.log(moment(futureDate).endOf('day').fromNow());
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
Second test utilizing difference calculation
moment.locale('de') // toggle between en and de -- english and german
var a = moment();
var b = moment(a).add(31, 'hours');
// Provides results in days
console.log(b.diff(a, 'days'));
console.log(b.diff(a, 'days', true));
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>