zoukankan      html  css  js  c++  java
  • Node和mysql配合时对时间的存储

    在mysql中通常我采用int类型来存储时间。int类型只有11位。

    但是js里我们通常可以用

    var nowDate=Date.now();

    来获取当前时间。js里的是13位。最后三位代表毫秒数。对于 对时间要求不是很严格的情况下 我们可以对它进行简单的处理来达到记录当前时间(精确到秒)的目的

    //获取当前时间戳
    global.getNow = function () {
        return parseInt(Date.now() / 1000);
    }
    /**
     * 格式化日期
     * @param  {[type]} date    [description]
     * @param  {[type]} pattern [description]
     * @return {[type]}         [description]
     */
    Date.format = function (date, pattern) {
        if (!date) {
            date = new Date;
        } else {
            if (!isDate(date)) {
                date = new Date(date);
            }
        }
        pattern = pattern || 'yyyy-MM-dd';
        var y = date.getFullYear().toString();
        var o = {
            M: date.getMonth() + 1, //month
            d: date.getDate(), //day
            h: date.getHours(), //hour
            m: date.getMinutes(), //minute
            s: date.getSeconds() //second
        };
        pattern = pattern.replace(/(y+)/ig, function (a, b) {
            return y.substr(4 - Math.min(4, b.length));
        });
        for (var i in o) {
            pattern = pattern.replace(new RegExp('(' + i + '+)', 'g'), function (a, b) {
                return (o[i] < 10 && b.length > 1) ? '0' + o[i] : o[i];
            });
        }
        return pattern;
    }
    /**
     * 获取日期和时间
     * @param  {[type]} date [description]
     * @return {[type]}      [description]
     */
    global.getDateTime = function (date) {
        //return php.date("Y-m-d h:m:s",date);
        return Date.format(date * 1000, "yyyy-MM-dd hh:mm:ss");
    }

    这样就可以用int这种简单的类型来存储时间了

  • 相关阅读:
    HDU 1010 Tempter of the Bone(DFS剪枝)
    HDU 1013 Digital Roots(九余数定理)
    HDU 2680 Choose the best route(反向建图最短路)
    HDU 1596 find the safest road(最短路)
    HDU 2072 单词数
    HDU 3790 最短路径问题 (dijkstra)
    HDU 1018 Big Number
    HDU 1042 N!
    NYOJ 117 求逆序数 (树状数组)
    20.QT文本文件读写
  • 原文地址:https://www.cnblogs.com/longhuang/p/3936834.html
Copyright © 2011-2022 走看看