话不多说,直接上代码
function ik_sjdfenge(time_dq,SJD,cishu) { function getTime(data){ var _data = data; //如果是13位正常,如果是10位则需要转化为毫秒 if (String(data).length == 13) { _data = data } else { _data = data*1000 } const time = new Date(_data); const Y = time.getFullYear(); const Mon = ik_pd(time.getMonth() + 1); const Day = ik_pd(time.getDate()); const H = ik_pd(time.getHours()); const Min = ik_pd(time.getMinutes()); const S = ik_pd(time.getSeconds()); function ik_pd(datas) { if(datas.toString().length == 1){ return "0" + datas; }else{ return datas; } } return `${Y}-${Mon}-${Day} ${H}:${Min}:${S}` } var SJDfg = SJD.split('-'); var time_on1 = time_dq + " " + SJDfg[0]; var time_on1s = parseInt(new Date(time_on1).getTime() / 1000); var time_on2 = time_dq + " " + SJDfg[1]; var time_on2s = parseInt(new Date(time_on2).getTime() / 1000) var xiangchabi = parseInt(3600 / cishu); var SJD_json = []; for(var i = 0;i<cishu;i++){ var dq_sjc = (xiangchabi * i) + time_on1s; SJD_json[i] = { 'id':i+1, 'time':getTime(dq_sjc), 'sjc':dq_sjc }; } return SJD_json; } console.log(ik_sjdfenge("2020-11-7","17:00:00-16:00:00",10));
返回值: