我有以下日期 -
201409290000
我试图使用以下内容拆分 -
var timePeriodDay = timePeriod.substring(6, Math.min(timePeriod.length, 2));
var timePeriodMonth = timePeriod.substring(4, Math.min(timePeriod.length, 2));
var timePeriodYear = timePeriod.substring(0, Math.min(timePeriod.length, 4));
var timePeriodTime = timePeriod.substring(8, Math.min(timePeriod.length, 4));
var timePeriodTime = [timePeriodTime.slice(0, 2), ":", timePeriodTime.slice(2)].join('');
然而,我的逻辑似乎已关闭,所需的输出应为 -
var timePeriodDay = 29
var timePeriodMonth = 09
var timePeriodYear = 2014
var timePeriodTime = 00:00
我该如何解决这个问题?
答案 0 :(得分:0)
尝试使用.substr
代替.substring
,因为您的第二个arg是要提取的字符数。
var timePeriod = '201409290000';
var timePeriodDay = timePeriod.substr(6, Math.min(timePeriod.length, 2));
var timePeriodMonth = timePeriod.substr(4, Math.min(timePeriod.length, 2));
var timePeriodYear = timePeriod.substr(0, Math.min(timePeriod.length, 4));
var timePeriodTime = timePeriod.substr(8, Math.min(timePeriod.length, 4));
var timePeriodTime = [timePeriodTime.slice(0, 2), ":", timePeriodTime.slice(2)].join('');
alert(timePeriodDay + ' ' + timePeriodMonth + ' ' + timePeriodYear + ' ' + timePeriodTime);

答案 1 :(得分:0)
你可以用这个
timePerio = '201409290000';
var timePeriodYear = timePerio.substr(0,4);
var timePeriodMonth =timePerio.substr(4,2);
var timePeriodDay = timePerio.substr(6,2);
var timePeriodTime = timePerio.substr(8,2)+':'+timePerio.substr(10,2);