首页 > Mockjs 如何做一个份带时间的数据

Mockjs 如何做一个份带时间的数据

想实现datetime 是一段时间, 或者指定时间自增长 这样的数据我该如何配置?

require(['mock','jquery'],function(Mock,$){
     var data = Mock.mock({
        'list|10': [{
            'value' : 1,
            'name' : '@DATETIME("yyyy-MM-dd HH:mm:ss")',
            'category':'@TITLE(1)'
        }]
     });

     var json  =JSON.stringify(data, null, 4);
     console.log(data.list)
     $('<pre>').text(json).appendTo('body')
})

输出结果目前是


    "list": [
        {
            "value": 1,
            "name": "2008-10-12 11:39:01",
            "category": "Kisxuicwl"
        },
        {
            "value": 1,
            "name": "2014-02-15 12:18:37",
            "category": "Qjin"
        },
        {
            "value": 1,
            "name": "2000-07-03 08:02:18",
            "category": "Qjige"
        },
        {
            "value": 1,
            "name": "2007-09-29 03:19:51",
            "category": "Fwuerpj"
        },
        {
            "value": 1,
            "name": "2003-12-05 03:55:22",
            "category": "Tjgofviend"
        },
        {
            "value": 1,
            "name": "1973-01-18 13:06:09",
            "category": "Ccewmmt"
        },
        {
            "value": 1,
            "name": "1992-12-16 14:08:16",
            "category": "Qxpy"
        },
        {
            "value": 1,
            "name": "1986-01-02 09:21:53",
            "category": "Nbll"
        },
        {
            "value": 1,
            "name": "1983-07-17 17:53:25",
            "category": "Wzxkltezo"
        },
        {
            "value": 1,
            "name": "2012-06-13 03:57:05",
            "category": "Emzbm"
        }
    ]
}

date.js

'use strict';
module.exports = function() {
  function rangeDate(min,max) {
    var min = min,
      max = max,
      days = (new Date(max) - new Date(min))/1000/60/60/24,
      i = 0,
      len = Math.floor(days),
      dates = [];

    for(;i<len;i++){
      dates.push(format(new Date(min).getTime()+1000*60*60*24*i));
    } 
    return  dates; 
  }

  function format(date) {
    var dateString = new Date(date),
        month = (dateString.getMonth()+1)<10 ? '0'+(dateString.getMonth()+1): (dateString.getMonth()+1),
        day = dateString.getDate()<10 ? '0'+dateString.getDate() : dateString.getDate();
    return dateString.getFullYear()+'-'+month+'-'+day
  }

  return rangeDate;
};

db.js

let rangeDate = require('./date')();
let dates = rangeDate('2016-05-01','2016-08-01');
【热门文章】
【热门文章】