时间格式化

var xdfJs = {}

xdfJs.time = function (data) {
function changeTime(data) {
data.time = new Date(data.time.replace('T', " ").replace(/-/g, '/')).getTime()
return data
}
function zero(number) {
return (number < 10) ? ("0" + number) : number.toString();
}
function getDate(data) {
 
var now = new Date(data.time),
y = zero(now.getFullYear()),
m = zero(now.getMonth() + 1),
d = zero(now.getDate());
return (data.y ? y + "-":'') + (data.m ? m + "-":'') + (data.d ? d :'') + " "+ now.toTimeString().substr(data.start, data.end);
}
var re = /^[0-9]+.?[0-9]*$/
if (Number(data.time) + '' != 'NaN') {
data.time = Number(data.time)
}
 
if (re.test(data.time)) {
return getDate(data);
} else {
return getDate(changeTime(data))
}
}
//用法 xdfJs.time(参数)
//下面是试测用例
var str = '2018-01-29T10:37'
var t = '1117163421040'
var info = {
"time": t,
"y":true,
"m":true,
"d":true,
"start":'0',//0开始为带小时数
"end":'8'// 8结束为带秒数
}
console.log(xdfJs.time(info), 234567)
posted @ 2019-02-25 15:20  陈健儿  阅读(124)  评论(0编辑  收藏  举报