这篇文章主要介绍了如何在Vue.js中将时间转换成时间戳,此处通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考价值,需要的朋友可以参考下:
Vue是一款友好的、多用途且高性能的JavaScript框架,使用vue可以创建可维护性和可测试性更强的代码库,Vue允许可以将一个网页分割成可复用的组件,每个组件都包含属于自己的HTML、CSS、JavaScript,以用来渲染网页中相应的地方,所以越来越多的前端开发者使用vue。
Date.prototype.format = function(format){
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
}
return format;
}
//使用方法
var now = new Date();
var nowStr = now.format("yyyy-MM-dd hh:mm:ss");
//使用方法2:
var testDate = new Date();
var testStr = testDate.format("YYYY年MM月dd日hh小时mm分ss秒");
alert(testStr);
//示例:
alert(new Date().format("yyyy年MM月dd日"));
alert(new Date().format("MM/dd/yyyy"));
alert(new Date().format("yyyyMMdd"));
alert(new Date().format("yyyy-MM-dd hh:mm:ss"));
代码:
// 格式化formatter中显示的时间格式
// Date.prototype.Format = function(fmt) {
// const o = {
// '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+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (`${this.getFullYear()}`).substr(4 - RegExp.$1.length)); }
// for (const k in o) {
// if (new RegExp(`(${k})`).test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((`00${o[k]}`).substr((`${o[k]}`).length))); }
// }
// return fmt;
// };
应用:
1、 2018-10-10 转 年月日
// new Date(this.envPlanList[i].starttime.slice(0, 4),
// this.envPlanList[i].starttime.slice(5, 7),
// this.envPlanList[i].starttime.slice(8, 10)),
// new Date(this.envPlanList[i].endtime.slice(0, 4),
// this.envPlanList[i].endtime.slice(5, 7),
// this.envPlanList[i].endtime.slice(8, 10)),
2、 年月日 转 2018-10-10
formatter(params) {
return `${params.name}: ${new Date(params.value[1]).Format('yyyy/MM/dd')} - - ${new Date(params.value[2]).Format('yyyy/MM/dd')} -- ${params.value[3]}`;
// return `${params.name}: ${params.value[1]} -- ${params.value[2]} -- ${params.value[3]}`;
},
下面看下vue.js时间戳转时间字符串
formartDate(param) {
let date = new Date(param);
Y = date.getFullYear() + '-';
M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '-' : date.getMonth() + 1 + '-';
D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
return Y + M + D + h + m + s;
}
到此这篇关于如何在Vue.js中将时间转换成时间戳的文章就介绍到这了,更多相关如何在Vue.js中将时间转换成时间戳的内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。