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

2019-11-13,,,


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 时间转换代码及时间戳转时间字符串,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对北冥有鱼网站的支持!

您可能感兴趣的文章:

  • javascript时间戳和日期字符串相互转换代码(超简单)
  • js获取时间并实现字符串和时间戳之间的转换
  • js实现把时间戳转换为yyyy-MM-dd hh:mm 格式(es6语法)
  • js时间戳与日期格式之间相互转换
  • JS获取时间的相关函数及时间戳与时间日期之间的转换
  • js与C#进行时间戳转换
  • js 时间格式与时间戳的相互转换示例代码
  • 时间戳转换为时间 年月日时间的JS函数
  • JS将时间秒转换成天小时分钟秒的字符串

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

下载本文的Word格式文档,以方便收藏与打印。