JavaScript How to add commas and “and” at the right place commas right now

Input: seconds, Output: grammatically correct, formatted time (with words spelled out).

So far I managed to get as far as going from say 40044373 to 1 year, 98 days, 5 hours, 37 minutes, 1 second - note the correct plurals and commas.

What I'm missing is an "and" that is added instead of the last comma (of course only when there's enough inputs). For example 1 year and 1 second for input 31556953 instead of my current 1 year, 1 second in that case.

function formatDuration (seconds) {

var numyears = Math.floor(seconds / 31556952);
if (numyears > 1) {var pluryears = " years, "} else {var pluryears = " year, "};
if (numyears > 0) {var printyears = numyears + pluryears;} else {var printyears = ''};

var numdays = Math.floor((seconds % 31556952) / 86400);
if (numdays > 1) {var plurdays = " days, "} else {var plurdays = " day, "};
if (numdays > 0) {var printdays = numdays + plurdays;} else {var printdays = ''};

var numhours = Math.floor(((seconds % 31556952) % 86400) / 3600);
if (numhours > 1) {var plurhours = " hours, "} else {var plurhours = " hour, "};
if (numhours > 0) {var printhours = numhours + plurhours;} else {var printhours = ''};

var numminutes = Math.floor((((seconds % 31556952) % 86400) % 3600) / 60);
if (numminutes > 1) {var plurminutes = " minutes, "} else {var plurminutes = " minute, "};
if (numminutes > 0) {var printminutes = numminutes + plurminutes;} else {var printminutes = ''};

var numseconds = (((seconds % 31556952) % 86400) % 3600) % 60;
if (numseconds > 1) {var plurseconds = " seconds"} else {var plurseconds = " second"};
if (numseconds > 0) {var printseconds = numseconds + plurseconds;} else {var printseconds = ''};

return(printyears + printdays + printhours + printminutes + printseconds)
}

formatDuration(31556953);
Answer:1

Just a little regex that replaces the last comma:

return (printyears + printdays + printhours + printminutes + printseconds)
    .replace(/, (.*)$/, " and $1");
Answer:2

I wrote a small library for precisely this purpose:

https://github.com/adamshaylor/compound-subject

In your case, you’d use it like:

var formattedTimeString = compoundSubject([
    numyears + 'years',
    numdays + 'days',
    numhours + 'hours',
    numminutes + 'minutes',
    numseconds + 'seconds'
]).make();

Yielding something like:

2 years, 5 days, 3 hours, 10 minutes and 2 seconds

You can also add an Oxford comma by calling delimitAll().

Answer:3

I searched for a solution of passing a get variable obtained from index.php page to included.php file[ loaded by javascript ]. A nice solution by php require function is given Pass and print a $_GET ...

I searched for a solution of passing a get variable obtained from index.php page to included.php file[ loaded by javascript ]. A nice solution by php require function is given Pass and print a $_GET ...

I want JQuery function to return me the value inside the 'data-tabconfig' <li class="tab-add" data-tabconfig="#{layoutBean.maxTabs}"> I can easily get it if I call onclick function. $(this)....

I want JQuery function to return me the value inside the 'data-tabconfig' <li class="tab-add" data-tabconfig="#{layoutBean.maxTabs}"> I can easily get it if I call onclick function. $(this)....

  1. find data value from percentile
  2. find data value jquery
  3. find data value
  4. find data value matlab
  5. r find value in dataframe
  6. find value in dataframe
  7. find value data r
  8. find value data type
  9. find data attribute value jquery
  10. find data-id value in jquery
  11. find percentile data value calculator
  12. find the data value that corresponds to each percentile
  13. javascript find data value
  14. find minimum data value
  15. find data set value
  16. how to find data value in statistics
  17. find element with data value jquery
  18. how to find data value with z score
  19. find max value data
  20. javascript find data attribute value

I am using extjs 5 on web app using sencha CMD layout (mvvm) and would like to bind a store (for labels) to filter list in my view grid the viewModel store definition is: stores: { labelContacts:...

I am using extjs 5 on web app using sencha CMD layout (mvvm) and would like to bind a store (for labels) to filter list in my view grid the viewModel store definition is: stores: { labelContacts:...

I am trying to mount/unmout a Pixi stage with React (I don't want to use react-pixi yet) I have an error when I re-mount the component: Uncaught Error: Resource with name "cupCake.png" already ...

I am trying to mount/unmout a Pixi stage with React (I don't want to use react-pixi yet) I have an error when I re-mount the component: Uncaught Error: Resource with name "cupCake.png" already ...