数据 格式化 format
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'));
轉載于:https://www.cnblogs.com/lily2021/p/6800490.html
總結
以上是生活随笔為你收集整理的数据 格式化 format的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c结构体里的数组与指针
- 下一篇: Maven打包时报Failed to e