欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Vue.js 时间转换代码及时间戳转时间字符串

程序员文章站 2022-06-03 11:59:08
date.prototype.format = function(format){ var o = { "m+" : this.getmonth()+1...

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 时间转换代码及时间戳转时间字符串,希望对大家有所帮助