te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to create an hours + minutes array with moment.js and ES6? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create an hours + minutes array with moment.js and ES6? - Stack Overflow

programmeradmin3浏览0评论

I'm trying to create an array of hours in a day with 30 minute intervals with moment.js and ES6.

Example: let hours = ["12:00 AM", "12:30 AM", "1:00 AM", "1:30 AM", ..., "11:30 PM"]

I already have this for function:

someFunction () {
  const items = []
  for (let hour = 0; hour < 24; hour++) {
    items.push(moment({ hour }).format('h:mm A'))
    items.push(moment({ hour, minute: 30 }).format('h:mm A'))
  }
  return items
}

But I would like to make it more ES6-like.

I have gotten this far:

someFunction () {
  let timeSlots = new Array(24).fill().map((acc, index) => {
    let items = []
    items.push(moment( index ).format('h:mm A'))
    items.push(moment({ index, minute: 30 }).format('h:mm A'))
  })
  return timeSlots
}

But it outputs:

["1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", ...]

I'm trying to create an array of hours in a day with 30 minute intervals with moment.js and ES6.

Example: let hours = ["12:00 AM", "12:30 AM", "1:00 AM", "1:30 AM", ..., "11:30 PM"]

I already have this for function:

someFunction () {
  const items = []
  for (let hour = 0; hour < 24; hour++) {
    items.push(moment({ hour }).format('h:mm A'))
    items.push(moment({ hour, minute: 30 }).format('h:mm A'))
  }
  return items
}

But I would like to make it more ES6-like.

I have gotten this far:

someFunction () {
  let timeSlots = new Array(24).fill().map((acc, index) => {
    let items = []
    items.push(moment( index ).format('h:mm A'))
    items.push(moment({ index, minute: 30 }).format('h:mm A'))
  })
  return timeSlots
}

But it outputs:

["1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", "1:00 AM", "12:30 AM", ...]

Share Improve this question asked Dec 20, 2018 at 11:28 DogeDoge 3871 gold badge9 silver badges30 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 6
function someFunction () {
  const items = [];
  new Array(24).fill().forEach((acc, index) => {
    items.push(moment( {hour: index} ).format('h:mm A'));
    items.push(moment({ hour: index, minute: 30 }).format('h:mm A'));
  })
  return items;
}

You can use array#from with array#reduce to generate 30 minutes interval time.

let someFunction = () => {
  return Array.from({length: 24}, (_,i) => i).reduce((r,hour) => {
     r.push(moment({hour, minute: 0}).format('h:mm A'));
     r.push(moment({hour, minute: 30}).format('h:mm A'));
     return r;
  }, []);
}
console.log(someFunction());
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.22.2/moment.min.js"></script>

here is working code:

someFunction = () => {
  var items = []
  var currentDate = moment('12');
  new Array(48).fill().map((acc, index) => {
    items.push(currentDate.format('h:mm A'))
    currenDate = currentDate.add(30, 'minutes');
  })
  return items
}

console.log(someFunction());

Working fiddle: http://jsfiddle/ekqxhwf4/1/

You could use Array.from method to generate 2d array and then concat with spread syntax to create 1d array.

function create () {
  return [].concat(...Array.from(Array(24), (_, hour) => ([
    moment({hour}).format('h:mm A'),
    moment({ hour, minute: 30 }).format('h:mm A')
  ])))
}

console.log(create())
<script src="http://momentjs./downloads/moment.js"></script>

I know momentJS was a requirement, but could also be easily solved with ordinary JavaScript Date objects:

function everyXMilliseconds(x) {
  if (x === void 0) {
    x = 86400000;
  }
  var base = new Date(86400000);
  var currentDate = new Date(86400000);
  var dates = [];
  while (currentDate.getUTCDate() === base.getUTCDate()) {
    dates.push(new Date(currentDate.getTime()));
    currentDate.setTime(currentDate.getTime() + x);
  }
  return dates;
}

function everyXSeconds(x) {
  if (x === void 0) {
    x = 86400;
  }
  return everyXMilliseconds(x * 1000);
}

function everyXMinutes(x) {
  if (x === void 0) {
    x = 1440;
  }
  return everyXSeconds(x * 60);
}

function everyXHours(x) {
  if (x === void 0) {
    x = 24;
  }
  return everyXMinutes(x * 60);
}
//Offsets date with its own timezone difference
function toLocalTime(date) {
  date.setUTCHours(date.getUTCHours() + date.getTimezoneOffset() / 60);
  return date;
}
//TEST
//get dates
var dates = everyXHours(0.5);
// dates to time
console.log('UTC times', dates.map(function(d) {
  return d.toLocaleTimeString("uk");
}));
// dates to time localized
console.log('Local times', dates.map(toLocalTime).map(function(d) {
  return d.toLocaleTimeString("uk");
}));

My snippet above is bit overly plicated, but it illustrates that you can make really flexible system without need to import libraries.

you can get the value with the label.

function someFunction () {
  const items = [];
  new Array(24).fill().forEach((acc, index) => {
    items.push({"value": moment( {hour: index} ).format('HH:mm'), "label": moment( {hour: index} ).format('h:mm A')});
    items.push({"value": moment({ hour: index, minute: 30 }).format('HH:mm'), "label": moment({ hour: index, minute: 30 }).format('h:mm A')});
  })
  return items;
}

An even shorter version would be:

const someFunction = () => 
  Array(48)
    .fill()
    .map((_, index) =>
      moment({
        hour: Math.floor(0.5 * index),
        minute: 30 * (index % 2)
      })
      .format("h:mm A"));
发布评论

评论列表(0)

  1. 暂无评论