V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chengyanwei
V2EX  ›  Node.js

记录一个 js 获取秒级时间戳的方法

  •  1
     
  •   chengyanwei · 2017-09-23 16:59:41 +08:00 · 13009 次点击
    这是一个创建于 2379 天前的主题,其中的信息可能已经有所发展或是发生改变。
    /**
    * 获取时间戳
    * @param date 日期:2017/03/03 13:49:55
    * @param num 当前日期的前或后推几年 /月 /天 /小时 /分钟 /秒
    * @param type yy 年 /mm 月 /dd 天 /h 小时 /m 分钟 /s 秒
    * @time 2017/08/31
    * @returns {string}
    */
    function timestamps (date = '', num = 0, type = 'dd') {
    let today;
    if (date == '' || date == 0) {
    today = Date.parse(new Date()).toString();
    } else {
    today = Date.parse(new Date(date)).toString();
    }
    let result = 0;
    if (num != 0 && num != '') {
    num = parseInt(num);
    if(type == 'yy' || type == 'mm') {
    let now;
    if (date == '' || date == 0) {
    now = new Date();
    } else {
    now = new Date(date);
    }
    let year = now.getFullYear();
    let month = parseInt(now.getMonth()) + 1;
    let day = now.getDate();
    let hours = now.getHours();
    let minutes = now.getMinutes();
    let seconds = now.getSeconds();
    if (type == 'yy') {
    year = parseInt(year) + num;
    } else {
    if (num > 0) {
    let m = parseInt(month) + num;
    if (m > 12) {
    let rem = m % 12;
    let remint = Math.floor(m / 12);
    if (rem != 0) {
    year = parseInt(year) + remint;
    month = rem;
    } else {
    year = parseInt(year) + remint -1;
    month = '12';
    }
    } else {
    month = m;
    }
    } else {
    let m = parseInt(month) + num;
    if (m == 0) {
    year = parseInt(year) - 1;
    month = '12';
    } else if (m > 0) {
    month = m;
    } else {
    if (m > -12) {
    month = parseInt(m) + 12;
    year = parseInt(year) - 1;
    } else {
    let rem = m % 12;
    let remint = parseInt(m / 12) - 1;
    month = parseInt(rem) + 12;
    year = parseInt(year) + remint;
    }
    }
    }
    }
    let format = this.getFormatDate(year, month, day);
    let res_date = format[0] + '/' + format[1] + '/' + format[2] + ' ' + hours + ':' + minutes + ':' + seconds;
    result = Date.parse(new Date(res_date)).toString();
    } else if (type == 'h') {
    result = today / 1000 + 3600 * num;
    return result.toString();
    } else if (type == 'm') {
    result = today / 1000 + 60 * num;
    return result.toString();
    } else if (type == 's') {
    result = today / 1000 + num;
    return result.toString();
    } else {
    result = today / 1000 + 3600 * 24 * num;
    return result.toString();
    }
    } else {
    result = today;
    }
    return Math.round(result / 1000);
    }
    9 条回复    2017-10-13 22:41:23 +08:00
    oott123
        1
    oott123  
       2017-09-23 22:37:08 +08:00
    Math.round(new Date / 1000)
    SilentDepth
        2
    SilentDepth  
       2017-09-23 23:27:33 +08:00
    楼主这个,用 code block 可好 = =
    chengyanwei
        3
    chengyanwei  
    OP
       2017-09-24 09:26:27 +08:00
    @SilentDepth 感谢指点,第一次发,没注意
    chengyanwei
        4
    chengyanwei  
    OP
       2017-09-24 09:27:59 +08:00
    @oott123 嗯嗯,主要是为了能加上一个某个时间往后推或者往前推这么一个功能,js 还在学习当中,感谢指点
    kyuuseiryuu
        5
    kyuuseiryuu  
       2017-09-24 20:52:02 +08:00 via iPhone
    moment 库?
    SilentDepth
        6
    SilentDepth  
       2017-09-24 21:35:48 +08:00
    @kyuuseiryuu #5 简单来说就是 Moment 的 Manipulate 功能
    connection
        7
    connection  
       2017-09-24 22:20:04 +08:00
    performance api.........
    chengyanwei
        8
    chengyanwei  
    OP
       2017-10-10 10:19:17 +08:00
    @SilentDepth @connection 了解了一下 moment.js 和 performance api 功能很强大,很有帮助,谢谢
    yemage
        9
    yemage  
       2017-10-13 22:41:23 +08:00
    +new Date()
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2770 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 12:02 · PVG 20:02 · LAX 05:02 · JFK 08:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.