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

  • 相关阅读:
    手机抓包方法
    IBM appscan 9.0破解版分享
    C#打开新页面
    双城记
    卸载趋势
    测试环境搭建
    C#常用函数→ASP.NET篇
    C#常用函数--通用篇
    读>>>>白帽子讲Web安全<<<<摘要→我推荐的一本书→1
    TCP/IP网络编程技术基础
  • 原文地址:https://www.cnblogs.com/open-coder/p/13835209.html
Copyright © 2011-2022 走看看