zoukankan      html  css  js  c++  java
  • 将时间戳转换成特定时间格式的实用函数

    后端接口返回的时间通常都是时间戳,而我们前端的童鞋们必须根据需求来展示成特定的格式。如 2018/09/04 10:28:38 或者 2018-09-04 10:28:38,如果你有这个需求就可以把我的函数copy走了。不说了上代码。

     /**
      * 
      * @param {Number} n: 需要补齐两位数的数字
      * @return {String} String: 前面用0补齐的两位或多位数字,字符类型。如: 2->02 12->12
      */
    function doubleNum(n) {
        const str = "" + n;
        // return str[1] ? str : `0${str}`;
       return str.padStart(2, "0"); }
    /** * * @param {*} timestamp 时间戳或者时间对象,以及可以转换成时间对象的字符串 * @param {String} sep 分隔符,如:"-"、"/" 默认是"-" * @return 日期格式的字符串 如:2018-09-04 2018/09/04 */ function date(timestamp, sep = "-") { var _date = new Date(timestamp); var year = _date.getFullYear(); var month = _date.getMonth() + 1; var day = _date.getDate(); return [year, month, day].map(doubleNum).join(sep); } /** * * @param {String} timestamp 时间戳或者时间对象,以及可以转换成时间对象的字符串 * @param {String} sep 分隔符,如:":" 默认是":" * @return 时间格式的字符串 如:10:52:08 */ function time(timestamp, sep = ":") { var _date = new Date(timestamp); var hours = _date.getHours(); var minutes = _date.getMinutes(); var seconds = _date.getSeconds(); return [hours, minutes, seconds].map(doubleNum).join(sep); } /** * * @param {*} timestamp 时间戳或者时间对象,以及可以转换成时间对象的字符串 * @param {String} dateSep 日期分隔符,如:"-"、"/" 默认是"-" * @param {String} timeSep 时间分隔符,如:":" 默认是":" * @return 时间格式的字符串 如:2018-09-04 10:52:08 */ function dateTime(timestamp, dateSep = "-", timeSep = ":") { return date(timestamp, dateSep) + " " + time(timestamp, timeSep); }

    如果不支持es6的语法,你可以稍微改一下代码,将es6改成es5的。代码如下:

     /**
      * 
      * @param {Number} n: 需要补齐两位数的数字
      * @return {String} String: 前面用0补齐的两位或多位数字,字符类型。如: 2->02 12->12
      */
    function doubleNum(n) {
        const str = "" + n;
        return str[1] ? str : "0"+str;
    }
    
    /**
     * 
     * @param {*} timestamp 时间戳或者时间对象,以及可以转换成时间对象的字符串
     * @param {String} sep 分隔符,如:"-"、"/"   默认是"-"
     * @return 日期格式的字符串 如:2018-09-04     2018/09/04
     */
    function date(timestamp, sep) {
        sep = sep || "-";
        var _date = new Date(timestamp);
        var year = _date.getFullYear();
        var month = _date.getMonth() + 1;
        var day = _date.getDate();
        return [year, month, day].map(doubleNum).join(sep);
    }
    
    /**
     * 
     * @param {String} timestamp 时间戳或者时间对象,以及可以转换成时间对象的字符串
     * @param {String} sep 分隔符,如:":"   默认是":"
     * @return 时间格式的字符串 如:10:52:08
     */
    function time(timestamp, sep) {
         sep = sep || ":";
        var _date = new Date(timestamp);
        var hours = _date.getHours();
        var minutes = _date.getMinutes();
        var seconds = _date.getSeconds();
        return [hours, minutes, seconds].map(doubleNum).join(sep);
    }
    
    /**
     * 
     * @param {*} timestamp 时间戳或者时间对象,以及可以转换成时间对象的字符串
     * @param {String} dateSep 日期分隔符,如:"-"、"/"   默认是"-"
     * @param {String} timeSep 时间分隔符,如:":"   默认是":"
     * @return 时间格式的字符串 如:2018-09-04 10:52:08
     */
    function dateTime(timestamp, dateSep, timeSep) {
        dateSep = dateSep || "-";
        timeSep = timeSep || ":";
        return date(timestamp, dateSep) + " " + time(timestamp, timeSep);
    }
  • 相关阅读:
    DRUPAL-PSA-CORE-2014-005 && CVE-2014-3704 Drupal 7.31 SQL Injection Vulnerability /includes/database/database.inc Analysis
    WDCP(WDlinux Control Panel) mysql/add_user.php、mysql/add_db.php Authentication Loss
    Penetration Testing、Security Testing、Automation Testing
    Tomcat Server Configuration Automation Reinforcement
    Xcon2014 && Geekpwn2014
    phpMyadmin /scripts/setup.php Remote Code Injection && Execution CVE-2009-1151
    Linux System Log Collection、Log Integration、Log Analysis System Building Learning
    The Linux Process Principle,NameSpace, PID、TID、PGID、PPID、SID、TID、TTY
    Windows Management Instrumentation WMI Security Technology Learning
    IIS FTP Server Anonymous Writeable Reinforcement, WEBDAV Anonymous Writeable Reinforcement(undone)
  • 原文地址:https://www.cnblogs.com/zhaodesheng/p/9583411.html
Copyright © 2011-2022 走看看