目 录CONTENT

文章目录
Vue

JS 基本

ABin
2023-12-07 / 0 评论 / 0 点赞 / 23 阅读 / 0 字

正则函数

this.userInfo.mobile.match(/^1[1-9]{1}[0-9]{9}$/g) == null

不足两位数补0

变量.toString().replace(/^(\d)$/,'0$1')

.toString().padStart(2, "0");

时间

var myDate = new Date();
myDate.getYear();  //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth();  //获取当前月份(0-11,0代表1月)
myDate.getDate();  //获取当前日(1-31)
myDate.getDay();   //获取当前星期X(0-6,0代表星期天)
myDate.getTime();  //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours();  //获取当前小时数(0-23)
myDate.getMinutes();  //获取当前分钟数(0-59)
myDate.getSeconds();  //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString();  //获取当前日期
var mytime=myDate.toLocaleTimeString();  //获取当前时间
myDate.toLocaleString( );  //获取日期与时间

// 获取当前时间戳(以s为单位)
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000;
//当前时间戳为:1403149534
console.log("当前时间戳为:" + timestamp);
 
// 获取某个时间格式的时间戳
var stringTime = "2014-07-10 10:21:12";
var timestamp2 = Date.parse(new Date(stringTime));
timestamp2 = timestamp2 / 1000;
//2014-07-10 10:21:12的时间戳为:1404958872 
console.log(stringTime + "的时间戳为:" + timestamp2);
 
// 将当前时间换成时间格式字符串
var timestamp3 = 1403058804;
var newDate = new Date();
newDate.setTime(timestamp3 * 1000);
// Wed Jun 18 2014 
console.log(newDate.toDateString());
// Wed, 18 Jun 2014 02:33:24 GMT 
console.log(newDate.toGMTString());
// 2014-06-18T02:33:24.000Z
console.log(newDate.toISOString());
// 2014-06-18T02:33:24.000Z 
console.log(newDate.toJSON());
// 2014年6月18日 
console.log(newDate.toLocaleDateString());
// 2014年6月18日 上午10:33:24 
console.log(newDate.toLocaleString());
// 上午10:33:24 
console.log(newDate.toLocaleTimeString());
// Wed Jun 18 2014 10:33:24 GMT+0800 (中国标准时间)
console.log(newDate.toString());
// 10:33:24 GMT+0800 (中国标准时间) 
console.log(newDate.toTimeString());
// Wed, 18 Jun 2014 02:33:24 GMT
console.log(newDate.toUTCString());

js获取时间戳精确到秒

// 获取当前时间戳(以s为单位)
var timestamp = Date.parse(new Date()) / 1000;
//当前时间戳为:1403149534
console.log("当前时间戳为:" + timestamp);

根据年份得到生肖

var index = year % 12
var animals = ["猴","鸡","狗","猪","鼠","牛","虎","兔","龙","蛇","马","羊"];
return animals[index];

根据月份获取星座

var c1='0123'    //获取文本框中的值
    var month=c1.substring(0,2);
    var day=parseInt(c1.substring(2));
    var x = '';
    switch(month){
        case "01":
            day > 19 ? x = '水瓶座' : x = '魔蝎座' 
            break;
        case "02":
            day > 18 ? x = '双鱼座' : x = '水瓶座' 
            break;
        case "03":
            day > 20 ? x = '白羊座' : x = '双鱼座' 
            break;
        case "04":
            day > 19 ? x = '金牛座' : x = '白羊座' 
            break;
        case "05":
            day > 20 ? x = '双子座' : x = '金牛座' 
            break;
        case "06":
            day > 21 ? x = '巨蟹座' : x = '双子座' 
            break;
        case "07":
            day > 22 ? x = '狮子座' : x = '巨蟹座' 
            break;
        case "08":
            day > 22 ? x = '处女座' : x = '狮子座' 
            break;
        case "09":
            day > 22 ? x = '天秤座' : x = '处女座' 
            break;
        case "10":
            day > 23 ? x = '天蝎座' : x = '天秤座' 
            break;
        case "11":
            day > 20 ? x = '射手座' : x = '天蝎座' 
            break;
        case "12":
            day > 21 ? x = '摩羯座' : x = '射手座' 
            break;
             
    }
    console.log(x,new Date())

从字符串中获取生日 (支持 1999 1 1)

#支持 19990101 1999 01 01 1999-01-01
#支持 1999-1-1  1999 1 1  1999-1-1
#不支持 199911
var date = 19990101 
var birthday = date.match(/^(\d{4}).{1}(\d{1,2}).{1}(\d{1,2}$)/);
var year = birthday[1]
var month = birthday[2].toString().padStart(2, "0");
var day = birthday[3].toString().padStart(2, "0");

console.log(year,month,day)

base64 图片旋转

#src =  bast64Data

rotateBase64Img(src,edg = 270) {
				var that = this
				var imageurl = ''
				  var canvas = document.createElement("canvas");
			      var ctx = canvas.getContext("2d");
			 
			      var imgW;//图片宽度
			      var imgH;//图片高度
			      var size;//canvas初始大小
			 
			      if (edg % 90 != 0) {
			          console.error("旋转角度必须是90的倍数!");
			          throw '旋转角度必须是90的倍数!';
			      }
			      (edg < 0) && (edg = (edg % 360) + 360)
			      const quadrant = (edg / 90) % 4; //旋转象限
			      const cutCoor = {sx: 0, sy: 0, ex: 0, ey: 0}; //裁剪坐标
			 
			      var image = new Image();
			      image.crossOrigin = "anonymous"
			      image.src = src;
			 
			      image.onload = function () {
			 
			          imgW = image.width;
			          imgH = image.height;
			          size = imgW > imgH ? imgW : imgH;
			 
			          canvas.width = size * 2;
			          canvas.height = size * 2;
			          switch (quadrant) {
			              case 0:
			                  cutCoor.sx = size;
			                  cutCoor.sy = size;
			                  cutCoor.ex = size + imgW;
			                  cutCoor.ey = size + imgH;
			                  break;
			              case 1:
			                  cutCoor.sx = size - imgH;
			                  cutCoor.sy = size;
			                  cutCoor.ex = size;
			                  cutCoor.ey = size + imgW;
			                  break;
			              case 2:
			                  cutCoor.sx = size - imgW;
			                  cutCoor.sy = size - imgH;
			                  cutCoor.ex = size;
			                  cutCoor.ey = size;
			                  break;
			              case 3:
			                  cutCoor.sx = size;
			                  cutCoor.sy = size - imgW;
			                  cutCoor.ex = size + imgH;
			                  cutCoor.ey = size + imgW;
			                  break;
			          }
			 
			 
			          ctx.translate(size, size);
			          ctx.rotate(edg * Math.PI / 180);
			          ctx.drawImage(image, 0, 0);
			 
			          var imgData = ctx.getImageData(cutCoor.sx, cutCoor.sy, cutCoor.ex, cutCoor.ey);
			          if (quadrant % 2 == 0) {
			              canvas.width = imgW;
			              canvas.height = imgH;
			          } else {
			              canvas.width = imgH;
			              canvas.height = imgW;
			          }
			          ctx.putImageData(imgData, 0, 0);
					  that.bast64img(canvas.toDataURL())
					  // console.log('im',imageurl)
					  
			      }
				  // console.log('image',imageurl)
				  // return imageurl
			    },
				bast64img(data){
					console.log('bast',data)
					// this.imageurl = data
					this.$emit('getImg', data)
					
					console.log(data, '148')
					uni.setStorageSync('tempFilePath',data)
					console.log(uni.getStorageSync('tempFilePath'));
					
					uni.navigateTo({
						url: '/minePages/Singlecommitment'
					})
				}

0

评论区