博客
关于我
js中时间戳转时间格式
阅读量:319 次
发布时间:2019-03-04

本文共 1394 字,大约阅读时间需要 4 分钟。

时间戳转时间格式方法

以下是一个将时间戳转换为指定格式的方法示例,供开发者参考。该方法可以根据需要灵活配置输出格式。

方法说明:

  • 方法名称:timestampToTimeFormat
  • 参数:
    • timestamp:需要转换的时间戳
    • format:格式化字符串,默认格式为'Y-M-D h:m'
  • 返回值:
    • 转换后的时间字符串
  • 示例:

    function timestampToTimeFormat(timestamp, format = 'Y-M-D h:m') {    var date = new Date(timestamp);    let obj = {        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())    };    let newitem = format.split('').map(item => {        for (let key in obj) {            if (item === key) {                item = obj[key];            }        }        return item;    });    return newitem.join("");}

    使用示例:

    // 默认格式console.log(timestampToTimeFormat(1572432123001)); // 2019-10-30 18:42// 自定义格式console.log(timestampToTimeFormat(1572432123001, 'Y-M-D')); // 2019-10-30console.log(timestampToTimeFormat(1572432123001, 'Y-M-D h:m:s')); // 2019-10-30 18:42:03console.log(timestampToTimeFormat(1572432123001, 'Y/M/D h:m:s')); // 2019/10/30 18:42:03console.log(timestampToTimeFormat(1572432123001, 'Y年M月D日 h:m:s')); // 2019年10月30日 18:42:03

    该方法支持多种时间格式输出,用户可根据需求自定义。

    转载地址:http://rqoq.baihongyu.com/

    你可能感兴趣的文章
    Postgres invalid command \N数据恢复处理
    查看>>
    Postgres like 模糊查询匹配集合
    查看>>
    Postgres 自定义函数内实现 in 操作符的递归查询
    查看>>
    Postgres 返回当前时间前后指定天数的集合
    查看>>
    postgres--vacuum
    查看>>
    postgres--wal
    查看>>
    postgres--流复制
    查看>>
    postgres10配置huge_pages
    查看>>
    PostgreSQL 10.0 preview sharding增强 - pushdown 增强
    查看>>
    PostgreSQL 10.0 preview 变化 - pg_xlog,pg_clog,pg_log目录更名为pg_wal,pg_xact,log
    查看>>
    PostgreSQL 10.1 手册_部分 II. SQL 语言_第 15章 并行查询_15.2. 何时会用到并行查询?...
    查看>>
    PostgreSQL 10.1 手册_部分 II. SQL 语言_第 9 章 函数和操作符_9.23. 行和数组比较
    查看>>
    PostgreSQL 10.1 手册_部分 III. 服务器管理_第 21 章 数据库角色
    查看>>
    Qt开发——网络编程UDP网络广播软件之服务器端
    查看>>
    Postgresql 12.9如何配置允许远程连接
    查看>>
    PostgreSQL 9.6 同步多副本 与 remote_apply事务同步级别 应用场景分析
    查看>>
    Postgresql CopyManager 流式批量数据入库
    查看>>
    PostgreSQL cube 插件 - 多维空间对象
    查看>>
    PostgreSQL Daily Maintenance - cluster table
    查看>>
    PostgreSQL on Linux 最佳部署手册
    查看>>