Get time difference as string "0h 0m 0s" - Node.js Date

Node.js examples for Date:Time

Description

Get time difference as string "0h 0m 0s"

Demo Code

  /**/* ww w . j  ava 2 s  .  c o  m*/
   * Get time difference as string "0h 0m 0s"
   * @param dateEariel date object
   * @param dateLater date object
   * @returns {string}
   */
  getTimeDiff: function( dateEariel, dateLater ) {
    if (dateEariel > dateLater) {
      dateLater.setDate(dateLater.getDate() + 1);
    }
    var msec = dateLater - dateEariel;
    var hh = Math.floor(msec / 1000 / 60 / 60);
    msec -= hh * 1000 * 60 * 60;
    var mm = Math.floor(msec / 1000 / 60);
    msec -= mm * 1000 * 60;
    var ss = Math.floor(msec / 1000);
    return ( hh > 0 ? hh + "h " : "" )
    + (mm > 0 ? mm + "m " : "" )
    + ss + "s";
  },

Related Tutorials