首页 > 其他 > 详细

封装时间戳转年月日时间插件

时间:2021-02-08 16:49:26      阅读:24      评论:0      收藏:0      [点我收藏+]

新建js文件

  1 /** 
  2  * 时间戳格式化函数 
  3  * @param  {string} format    格式 
  4  * @param  {int}    timestamp 要格式化的时间 默认为当前时间 
  5  * @return {string}           格式化的时间字符串 
  6  *   8  */
  9 export function date(format, timestamp){  
 10     var a,ret, jsdate=((timestamp) ? new Date(timestamp*1000) : new Date()); 
 11     var pad = function(n, c){ 
 12         if((n = n + "").length < c){ 
 13             return new Array(++c - n.length).join("0") + n; 
 14         } else { 
 15             return n; 
 16         } 
 17     }; 
 18     var txt_weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; 
 19     var txt_ordin = {1:"st", 2:"nd", 3:"rd", 21:"st", 22:"nd", 23:"rd", 31:"st"}; 
 20     var txt_months = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];  
 21     var f = { 
 22         // Day 
 23         d: function(){return pad(f.j(), 2)}, 
 24         D: function(){return f.l().substr(0,3)}, 
 25         j: function(){return jsdate.getDate()}, 
 26         l: function(){return txt_weekdays[f.w()]}, 
 27         N: function(){return f.w() + 1}, 
 28         S: function(){return txt_ordin[f.j()] ? txt_ordin[f.j()] : ‘th‘}, 
 29         w: function(){return jsdate.getDay()}, 
 30         z: function(){return (jsdate - new Date(jsdate.getFullYear() + "/1/1")) / 864e5 >> 0}, 
 31         
 32         // Week 
 33         W: function(){ 
 34             var a = f.z(), b = 364 + f.L() - a; 
 35             var nd2, nd = (new Date(jsdate.getFullYear() + "/1/1").getDay() || 7) - 1; 
 36             if(b <= 2 && ((jsdate.getDay() || 7) - 1) <= 2 - b){ 
 37                 return 1; 
 38             } else{ 
 39                 if(a <= 2 && nd >= 4 && a >= (6 - nd)){ 
 40                     nd2 = new Date(jsdate.getFullYear() - 1 + "/12/31"); 
 41                     return date("W", Math.round(nd2.getTime()/1000)); 
 42                 } else{ 
 43                     return (1 + (nd <= 3 ? ((a + nd) / 7) : (a - (7 - nd)) / 7) >> 0); 
 44                 } 
 45             } 
 46         }, 
 47         
 48         // Month 
 49         F: function(){return txt_months[f.n()]}, 
 50         m: function(){return pad(f.n(), 2)}, 
 51         M: function(){return f.F().substr(0,3)}, 
 52         n: function(){return jsdate.getMonth() + 1}, 
 53         t: function(){ 
 54             var n; 
 55             if( (n = jsdate.getMonth() + 1) == 2 ){ 
 56                 return 28 + f.L(); 
 57             } else{ 
 58                 if( n & 1 && n < 8 || !(n & 1) && n > 7 ){ 
 59                     return 31; 
 60                 } else{ 
 61                     return 30; 
 62                 } 
 63             } 
 64         }, 
 65         
 66         // Year 
 67         L: function(){var y = f.Y();return (!(y & 3) && (y % 1e2 || !(y % 4e2))) ? 1 : 0}, 
 68         //o not supported yet 
 69         Y: function(){return jsdate.getFullYear()}, 
 70         y: function(){return (jsdate.getFullYear() + "").slice(2)}, 
 71         
 72         // Time 
 73         a: function(){return jsdate.getHours() > 11 ? "pm" : "am"}, 
 74         A: function(){return f.a().toUpperCase()}, 
 75         B: function(){ 
 76             // peter paul koch: 
 77             var off = (jsdate.getTimezoneOffset() + 60)*60; 
 78             var theSeconds = (jsdate.getHours() * 3600) + (jsdate.getMinutes() * 60) + jsdate.getSeconds() + off; 
 79             var beat = Math.floor(theSeconds/86.4); 
 80             if (beat > 1000) beat -= 1000; 
 81             if (beat < 0) beat += 1000; 
 82             if ((String(beat)).length == 1) beat = "00"+beat; 
 83             if ((String(beat)).length == 2) beat = "0"+beat; 
 84             return beat; 
 85         }, 
 86         g: function(){return jsdate.getHours() % 12 || 12}, 
 87         G: function(){return jsdate.getHours()}, 
 88         h: function(){return pad(f.g(), 2)}, 
 89         H: function(){return pad(jsdate.getHours(), 2)}, 
 90         i: function(){return pad(jsdate.getMinutes(), 2)}, 
 91         s: function(){return pad(jsdate.getSeconds(), 2)}, 
 92         //u not supported yet 
 93         
 94         // Timezone 
 95         //e not supported yet 
 96         //I not supported yet 
 97         O: function(){ 
 98             var t = pad(Math.abs(jsdate.getTimezoneOffset()/60*100), 4); 
 99             if (jsdate.getTimezoneOffset() > 0) t = "-" + t; else t = "+" + t; 
100             return t; 
101         }, 
102         P: function(){var O = f.O();return (O.substr(0, 3) + ":" + O.substr(3, 2))}, 
103         //T not supported yet 
104         //Z not supported yet 
105         
106         // Full Date/Time 
107         c: function(){return f.Y() + "-" + f.m() + "-" + f.d() + "T" + f.h() + ":" + f.i() + ":" + f.s() + f.P()}, 
108         //r not supported yet 
109         U: function(){return Math.round(jsdate.getTime()/1000)} 
110     }; 
111         
112     return format.replace(/[\\]?([a-zA-Z])/g, function(t, s){ 
113         if( t!=s ){ 
114             // escaped 
115             ret = s; 
116         } else if( f[s] ){ 
117             // a date function exists 
118             ret = f[s](); 
119         } else{ 
120             // nothing special 
121             ret = s; 
122         } 
123         return ret; 
124     }); 
125 }

直接引入或者全局挂载

import {date} from ‘util/getDate.js‘
Vue.prototype.date = date

调用方法

date(‘Y-m-d‘,‘1612764711‘);//很方便的将时间戳转换成了2021-02-08
date(‘Y-m-d H:i:s‘,‘1612764711‘);//得到的结果是2021-02-08 14:11:51


或者直接在view视图调用
<view class=" w100s">时间:{{date(‘Y-m-d H:i‘,item.createTime/1000)}}</view>

 

封装时间戳转年月日时间插件

原文:https://www.cnblogs.com/pingtouha/p/14388665.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!