zoukankan      html  css  js  c++  java
  • NodeJS 获取系统时间 并格式化输出

    1. 安装 silly-datetime 模块:

    npm i silly-datetime --save

    2. 使用的示例代码:

    var sd = require('silly-datetime');
     
    // silly-datetime 当前时间格式化
    console.log(sd.format(new Date(), 'YYYY-MM-DD HH:mm'));
    // 2019-10-28 12:41
    console.log(sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss'));
    // 2019-10-28 12:41:14
     
    // 通过时间戳拼接 格式化时间
    var date = new Date((new Date()).getTime())
        Y = date.getFullYear() + '-';
        M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
            D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' ';
            h = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours()) + ':';
            m = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes()) + ':';
            s = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());
    console.log(Y+M+D+h+m+s);
    // 2019-10-28 12:41:14
        
    // 获取当前时间戳
    console.log((new Date()).getTime());
    // 1572237674282
    console.log((new Date()).valueOf());
    // 1572237674283
    console.log(Date.parse(new Date()));
    // 1572237674000
     
    // 将指定时间转化成时间戳
    var newDate = new Date(sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss'));
    console.log(newDate.getTime());

    Reference: https://blog.csdn.net/wx19900503/article/details/102785652

  • 相关阅读:
    codeforce1028A Find Square
    2018ccpc网络赛 Buy and Resell
    差分约束
    Lost Cows
    动态查询区间第k大
    疫情控制
    天天爱跑步
    次小生成树
    树上差分闇の連锁
    Stars in Your Window
  • 原文地址:https://www.cnblogs.com/open-coder/p/13835209.html
Copyright © 2011-2022 走看看