Return Date object as string format to YYY-MM-DD, YYY-MM-DD hh:mm:ss - Node.js Date

Node.js examples for Date:Date Format

Description

Return Date object as string format to YYY-MM-DD, YYY-MM-DD hh:mm:ss

Demo Code

var Utils = {/*from   w w w .  jav a2 s.c om*/
  /**
   * Return Date object as string formated to YYY-MM-DD
   * @param date
   * @returns {string}
   */
  formatDate: function (date) {
    return date.getFullYear() + "-"
    + ( ( date.getMonth() + 1 ) < 10 ? 0 : "" )
    + ( date.getMonth() + 1 ) + "-"
    + ( ( date.getDate() ) < 10 ? 0 : "" )
    + date.getDate();
  },
  /**
   * Return Date object as string formated to YYY-MM-DD hh:mm:ss
   * @param date
   * @returns {string}
   */
  formatDateTime: function (date) {
    return Utils.formatDate(date) + " "
    + ( date.getHours() < 10 ? 0 : "" )
    + date.getHours() + ":"
    + ( date.getMinutes() < 10 ? 0 : "" )
    + date.getMinutes() + ":"
    + ( date.getSeconds() < 10 ? 0 : "" )
    + date.getSeconds();
  },

Related Tutorials