[英]Create an array of the last 24 hours using moment.js
我试图在Moment.js中创建一个过去24小时的数组,其值仅是小时数,然后是AM / PM(而不是分钟,秒甚至日历日期)。
它现在可以工作,但是当我只需要几小时的时间时,会在每个元素中给出整个日期数据(Sun May 06 2018 22:24:29 GMT-0700):
JS代码:
var hoursPerDay = 24; var time = []; function timeOneDay() { var formattedTime; for (i = 0; i < hoursPerDay + 1; i++) { //fill in all of the hours formattedTime = (moment().subtract(i, "hours")); formattedTime.format("hA"); //give the time in format X AM/PM time.unshift(formattedTime); //add to beginning of array } //do this for all 24 hours } timeOneDay(); console.log(time);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
moment.format()
返回格式化的值。 您没有将其存储在变量中
var hoursPerDay = 24; var time = []; function timeOneDay(){ var formattedTime; for(i =0; i < hoursPerDay+1 ; i++){ //fill in all of the hours formattedTime = (moment().subtract(i, "hours")).format("hA"); //give the time in format X AM/PM time.unshift(formattedTime); //add to beginning of array } //do this for all 24 hours } timeOneDay(); console.log(time);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.