我是勤劳的代码搬运工
复制
方法:
copyToClipboard(text){
return navigator.clipboard.writeText(text);
},
调用:
this.copyToClipboard("Hello world");
检查日期是否合法
方法:
const isDateValid = (...val) => !Number.isNaN(new Date(...val).valueOf());
调用:
isDateValid("December 17, 1995 03:24:00");
// Result: true
计算两个日期之间相差多少天
方法:
const dayDif = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000)
调用:
dayDif(new Date("2020-10-21"), new Date("2021-10-22"))
// Result: 366
查找日期位于一年中的第几天
方法:
const dayOfYear = (date) =>
Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);
调用:
dayOfYear(new Date());
// Result: 272
获取路径查询参数
const getParameters = (URL) => {
URL = JSON.parse(
'{"' +
decodeURI(URL.split("?")[1])
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"') +
'"}'
);
return JSON.stringify(URL);
};
调用:
getParameters(window.location);
// Result: { search : "easy", page : 3 }
数字取整
let floatNum = 100.5;
let intNum = ~~floatNum;
console.log(intNum); // 100
H5语音合成
<!DOCTYPE html>
<input id="btn1" type="button" value="点我" onclick="test();" />
<script>
function test() {
const sos = `阿尤!不错哦`;
const synth = window.speechSynthesis;
let msg = new SpeechSynthesisUtterance(sos);
synth.speak(msg)
}
</script>
解决苹果手机打开静音键情况下,语音播放也静音问题
// #ifdef MP-WEIXIN
wx.setInnerAudioOption({
obeyMuteSwitch: false,
});
// #endif
网友评论