关于日期时间的操作

时间处理过滤器(vue和uni)

// 时间格式过滤器
Vue.filter("formatDate",(data)=>{
	const nDate=new Date(data*1000);
	const year = nDate.getFullYear().toString().padStart(2,0);
	const month = (nDate.getMonth()+1).toString().padStart(2,0);
	const day = nDate.getDate().toString().padStart(2,0);
	const hour = nDate.getHours().toString().padStart(2,0);
	const minute = nDate.getMinutes().toString().padStart(2,0);
	const second = nDate.getSeconds().toString().padStart(2,0);
	return month +'-'+ day+' '+hour+':'+minute
})
//用法
{{ item.appointment_time | formatDate }} 

随机命名(当前时间戳+4位随机数)

new Date().getTime() + "" + Math.round(Math.random() * 10000
1.Math.random();        结果为0-1间的一个随机数(包括0,不包括1)
2.Math.floor(num);      向下取整
3.Math.round(num);    四舍五入后取整

获取今天0点和23.59.59时间

const end = new Date(new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1);
const start = new Date(new Date(new Date().toLocaleDateString()).getTime());
console.log(start);

获取当前日期

function getNowFormatDate() {
		// 格式化今天日期
       	var t = new Date();
    	var y = t.getFullYear();
	    var m= t.getMonth()+1>9?t.getMonth()+1:`0${t.getMonth()+1}`;     //两位数
 	    var d = t.getDate()>9?t.getDate():`0${t.getDate()}`;
 	
	    var h= t.getHours(); //获取当前小时数(0-23)
	    var mm= t.getMinutes(); //获取当前分钟数(0-59)
	    var s= t.getSeconds(); //获取当前秒数(0-59)
		 return  y+'-'+m+'-'+d ;
    }

日期转时间戳

//将当前的时间转换成时间戳
var now = new Date();
console.log(now.getTime());
//将指定的日期转换成时间戳
var t = "2018-07-23 20:5:30";  
var T = new Date(t); 
console.log(T.getTime());

获取验证码倒计时

 //   倒计时
      let timerId = setInterval(() => {
        this.time -= 1;
        if (this.time <= 0) {
          clearInterval(timerId);
          this.timeover = true;
          this.time = 60;
        }
      }, 1000);

已知用户选择的时间段(x月),求截止日期

 function time(num){
    var myDate = new Date();//获取系统当前时间
    var y = myDate.getFullYear(); //获取当前年份(2位)
    var m=myDate.getMonth()+1; //获取当前月份(0-11,0代表1月)
    var d=myDate.getDate(); //获取当前日(1-31)
    if(m+num<=12){
      m=m+num;
    }else{
      y = y + ((m + num) % 12 == 0 ? parseInt((m + num) / 12)-1 : parseInt((m+num)/12));
      m = (m + num) % 12==0 ? 12:(m + num) % 12 
    }
    return  y+'-'+m+'-'+d ;
  }

时间戳转日期格式

 getTime(timestamp) {
      var date = new Date(timestamp * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
      var Y = date.getFullYear();
      var M =
        date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1;
      var D = date.getDate();
      return Y + "-" + M + "-" + D;
    }

字符串形式日期格式化(20190202)

getTime(time) {
      return time.substr(0, 4) + "-" + time.substr(4, 2) + "-" + time.slice(6);
    }

10位时间戳转倒计时时分秒

getTime(t) {
    console.log(t);
    let h = this.addZero(parseInt(t / 60 / 60))
    let m = this.addZero(parseInt(t / 60 % 60))
    let s = this.addZero(parseInt(t % 60))
    console.log(h, m, s)
  },

不足两位加0

 addZero(n) {
    console.log(n);
    return n = n < 10 ? "0" + n : n
  },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小曲曲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值