有没有一种方法可以将unix格式的时间戳显示为ISODate? [英] Is there a way to display timestamp in unix format to ISODate?

查看:66
本文介绍了有没有一种方法可以将unix格式的时间戳显示为ISODate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用unix时间戳将日期存储在MongoDB中,执行查询时如何获取日期?有没有办法以ISODate格式显示时间戳?

We stored a date using unix timestamp in MongoDB, how do I get the date when I do the query? Is there a way to display timestamp in ISODate format?

推荐答案

背景

  • unixtime 值表示自时代以来的 seconds (1970年1月1日).

    Background

    • A unixtime value represents seconds since the epoch (Jan 1, 1970).

      A JavaScript Date()代表自纪元以来的毫秒.

      A JavaScript Date() represents milliseconds since the epoch.

      在MongoDB中, ISODate() Date()的便捷包装,它允许您从mongo外壳程序中的ISO字符串创建日期.如果在外壳中使用new Date(),它将返回ISODate().

      In MongoDB, ISODate() is a convenience wrapper for Date() that allows you to create dates from ISO strings in the mongo shell. If you use new Date() in the shell, it will return an ISODate().

      要在unixtime和ISODate()之间进行转换,可以将Unix时间戳乘以1000,然后将此值传递给new Date()构造函数.

      To convert between a unixtime and an ISODate() you can multiply your unix timestamps by 1000 and pass this value to the new Date() constructor.

      > db.mydata.insert({
          unixtime: 1362143511
      })
      
      > var doc = db.mydata.findOne();
      
      // convert unixtime seconds to milliseconds and create JS date
      > var date = new Date(doc.unixtime * 1000);
      
      > date
      ISODate("2013-03-01T13:11:51Z")
      

      这篇关于有没有一种方法可以将unix格式的时间戳显示为ISODate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆