<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<input type="" name="" id="uppercase" value="" />
<div id="num">
</div>
</body>
<script type="text/javascript">
function zwTransformNum(str) {
let numStr = "一二三四五六七八九十";
let yearStr = ``;
let year = ``;
let monthStr = ``;
let month = ``;
let dateStr = ``;
let date = ``;
if (str.indexOf("年") != -1) {
yearStr = str.substring(0, str.indexOf("年"));
if (yearStr.length == 3) {
year += numStr.indexOf(yearStr.substring(0, 1)) + 1;
year += numStr.indexOf(yearStr.substring(2, 3)) + 1;
year += "年";
} else if (yearStr.length == 2) {
year += "1";
year += numStr.indexOf(yearStr.substring(1, 2)) + 1;
year += "年";
} else if (yearStr.length == 1) {
year += numStr.indexOf(yearStr.substring(0, 1)) + 1;
year += "年";
}
if (str.indexOf("月") != -1) {
monthStr = str.substring(str.indexOf("年") + 1, str.indexOf("月"));
if (monthStr.length == 3) {
month += "1";
month += numStr.indexOf(monthStr.substring(1, 2)) + 1;
month += "个月";
} else if (monthStr.length == 2) {
month += numStr.indexOf(monthStr.substring(0, 1)) + 1;
month += "个月";
}
if (str.indexOf("天") != -1) {
dateStr = str.substring(str.indexOf("月") + 1, str.indexOf("天"));
if (dateStr.length == 3) {
date += numStr.indexOf(dateStr.substring(0, 1)) + 1;
date += numStr.indexOf(dateStr.substring(2, 3)) + 1;
date += "天";
} else if (dateStr.length == 2) {
date += "1";
date += numStr.indexOf(dateStr.substring(1, 2)) + 1;
date += "天";
} else if (dateStr.length == 1) {
date += numStr.indexOf(dateStr.substring(0, 2)) + 1;
date += "天";
}
}
}
} else if (str.indexOf("月") != -1) {
monthStr = str.substring(0, str.indexOf("月"));
if (monthStr.length == 3) {
month += "1";
month += numStr.indexOf(monthStr.substring(1, 2)) + 1;
month += "个月";
} else if (monthStr.length == 2) {
month += numStr.indexOf(monthStr.substring(0, 1)) + 1;
month += "个月";
}
if (str.indexOf("天") != -1) {
dateStr = str.substring(str.indexOf("月") + 1, str.indexOf("天"));
if (dateStr.length == 3) {
date += numStr.indexOf(dateStr.substring(0, 1)) + 1;
date += numStr.indexOf(dateStr.substring(2, 3)) + 1;
date += "天";
} else if (dateStr.length == 2) {
date += "1";
date += numStr.indexOf(dateStr.substring(1, 2)) + 1;
date += "天";
} else if (dateStr.length == 1) {
date += numStr.indexOf(dateStr.substring(0, 2)) + 1;
date += "天";
}
}
} else if (str.indexOf("天") != -1) {
dateStr = str.substring(0, str.indexOf("天"));
if (dateStr.length == 3) {
date += numStr.indexOf(dateStr.substring(0, 1)) + 1;
date += numStr.indexOf(dateStr.substring(2, 3)) + 1;
date += "天";
} else if (dateStr.length == 2) {
date += "1";
date += numStr.indexOf(dateStr.substring(1, 2)) + 1;
date += "天";
} else if (dateStr.length == 1) {
date += numStr.indexOf(dateStr.substring(0, 2)) + 1;
date += "天";
}
}
console.log(year + month + date)
document.getElementById("num").innerHTML = year + month + date;
}
document.getElementById("uppercase").oninput = function() {
console.log(document.getElementById("uppercase").value)
zwTransformNum(document.getElementById("uppercase").value);
}
</script>
</html>
js把年月天(十九年十一个月二十八天)转为数字格式(19年11个月28天)
最新推荐文章于 2023-08-01 15:46:15 发布