• 请不要在回答技术问题时复制粘贴 AI 生成的内容
yantianqi
V2EX  ›  程序员

node.js 时间格式化问题

  •  
  •   yantianqi · Dec 4, 2017 · 4631 views
    This topic created in 3258 days ago, the information mentioned may be changed or developed.

    node.js 存数据库用的 Date.now
    当取出时间的时候格式如下 Mon Dec 04 2017 15:27:17 GMT+0800 (中国标准时间)
    怎么格式化时间呢? 我看网上说用 moment.js
    是存进去的时候格式化,还是取出来后格式化?

    10 replies  •  2017-12-05 10:09:58 +08:00
    wxsm
    wxsm
    Dec 4, 2017
    1
    存一般都只存时间戳或者 date 类型,具体想要怎么显示取出来以后可以随意操作。不会“在存进去的时候格式化”的。
    yantianqi
    yantianqi
    OP
    Dec 4, 2017
    2
    @wxsm 如何在取出数据后,处理数据呢?谢谢
    kohos
    kohos
    Dec 4, 2017
    3
    把日期打印出来都是这种格式,默认调用了 toString 方法。不想用其他库的话调用 toISOString 可以返回 2017-12-04T14:51:06.157Z 这样的格式,替换一下就能变常用的格式了
    hazyzh
    hazyzh
    Dec 4, 2017
    4
    var a = new moment('Mon Dec 04 2017 15:27:17 GMT+0800')
    a.format('YYYY-MM-DD hh:mm:ss')

    不大多都是这样么
    vinsony
    vinsony
    Dec 4, 2017
    5
    还是用 moment 吧
    yantianqi
    yantianqi
    OP
    Dec 4, 2017
    6
    @vinsony 用 moment 怎么用呢?
    一般是在 view 层循环来调时间的
    lianyue
    lianyue
    Dec 4, 2017
    7
    存进去? mongodb 里面?
    jimzhong
    jimzhong
    Dec 4, 2017
    9
    还是用 moments 吧
    solee
    solee
    Dec 5, 2017
    10
    我们时间数据都存的 unix timestamp 方便比较查询和索引。取出来用 moment format 就好。存进去对我们来说用原生和 moment 都是一样的,转成 unix timestamp
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2912 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 62ms · UTC 11:27 · PVG 19:27 · LAX 03:27 · JFK 06:27
    ♥ Do have faith in what you're doing.