JSFiddle - React, Tailwind, and code Playground
by zhangchi
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<h6>将 YYYY-MM-DD 转化为 YYYY年M月D日</h1>
<input type="text" id="ttt"/>
<div id="bbb" style="margin:10px 0;border:1px solid #333;width:200px;height:30px">result</div>
<hr/>
JavaScript
document.getElementById('ttt').addEventListener('change', function() {
var inputValue = document.getElementById('ttt').value;
var finalValue = moment(new Date(inputValue)).format('YYYY年M月D日');
document.getElementById('bbb').innerHTML = finalValue
copyToClipboard(finalValue);
})
function copyToClipboard(str) {
const el = document.createElement('textarea'); //创建input对象
el.value = str;
el.setAttribute('readonly', ''); //当前获得焦点的元素
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el); //添加元素
const selected = document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
el.select();
document.execCommand('copy'); //执行复制
document.body.removeChild(el);//删除元素
if (selected) {
document.getSelection().removeAllRanges();
document.getSelection().addRange(selected);
}
};