我有两个 javascript 变量:startDate
和 endDate
。我想将当前时间(以毫秒为单位的 unix 时间戳)存储到 endDate
中,并将 24 小时前的时间戳存储在 startDate
中。我将使用这两个变量来查询最近 1 天的记录。我如何使用 moment JS 做到这一点?
最佳答案
你看过这个网站了吗?它充满了例子 - http://momentjs.com/我认为你要做的很简单
var startDate = moment(endDate).subtract(1, 'days');
根据您的问题,您可以这样做:
var endDate = moment(); //the current time
或者,您可以忽略这个问题的 endDate 部分,直接使用 startDate
var startDate = moment().subtract(1, 'days'); //one day before the current time
最后,如果您需要以某种方式格式化它,您可以应用格式化规则,例如:
moment().subtract(1,'days').format('YYYY-MM-DD h:mm:ss a')
使用不带参数的格式,它会为您提供 ISO 8601 格式
moment().subtract(1,'days').format() //eg: "2015-04-04T01:53:26-05:00"
关于javascript - 如何使用 Moment JS 获取时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29454824/