数据 格式化 format
2017-05-03 10:14 lily202121 阅读(192) 评论(0) 编辑 收藏 举报1. 保留两位小数
fCorrectRate: function(double) {
return Math.round(double * 100)
}
如:
"correctRate": 0.95156
改成
2. 取文本最后一个文字
fShowLastText: function(text) {
return text.charAt(text.length - 1);
}
3.字段转化成文本
fLangCn: function(key) {
var _object = {
"TeachingClasses": "已开课",
"FinishedClasses": "已结课"
}
return _object[key];
}
4.时间取前10位
fnYearMonthDay = function() {
return this.SendTime.substring(0, 10);
}
5.秒化成分钟
/*
* 将秒数格式化时间
* @param {Number} seconds: 整数类型的秒数
* @return {String} time: 格式化之后的时间
*/
function formatTime(seconds) {
var min = Math.floor(seconds / 60),
second = seconds % 60,
hour, newMin, time;
if (min > 60) {
hour = Math.floor(min / 60);
newMin = min % 60;
}
if (second < 10) { second = '0' + second;}
if (min < 10) { min = '0' + min;}
return time = hour? (hour + ':' + newMin + ':' + second) : (min + ':' + second);
}
js把时间戳转为普通格式的方法。
方法1,
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
}
alert(getLocalTime(1293072805));
结果:
2010年12月23日 10:53
方法2:
return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17)}
alert(getLocalTime(1293072805));
要得到如下的日期格式:
2010-10-20 10:00:00
var timestamp3 = 1403058804;
var newDate = new Date();
Date.prototype.format = function(format) {
var date = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S+": this.getMilliseconds()
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1
? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
}
console.log(newDate.format('yyyy-MM-dd h:m:s'));