Date-conv-zh.php
Here's a page for converting Common Era years and 天干地支纪年法 in both directions. BCEs are currently unavailable.
<!DOCTYPE html>
<html>
<head>
<title>公元纪年&天干地支纪年转换</title>
<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=0.8">
<style>
@media (max-width: 980px) {
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #007eca;
}
}
@media (min-width: 981px) {
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #007eca;
background-image: url("https://dl.itslittlekevin.com/projects/5069.png");
background-repeat: no-repeat;
background-position: right center;
background-size: auto 100%;
}
}
h1, p, h4 {
text-shadow: 2px 2px 2px #00000055;
}
* {
color: white;
}
button {
font-size: 15px;
background-color: #00325565;
padding: 5px 15px;
border-radius: 5px;
border-width: 0px;
margin: 5px;
cursor: pointer;
}
h4 a {
text-decoration: none;
}
input {
color: black;
}
h1 {
text-align: center;
}
input[type="number"] {
color: black;
margin: 10px;
border-radius: 5px;
padding: 5px 15px
}
input[type="text"] {
color: black;
margin: 10px;
border-radius: 5px;
padding: 5px 15px
}
}
button {
font-size: 15px;
margin: 10px;
}
p {
font-size: 15px;
margin: 10px;
}
</style>
</head>
<body>
<h1>公元纪年&天干地支纪年转换</h1>
<p>输入公元年或天干地支年后点击"转换"按钮,即可查看对应相应年份。</p>
<p>天干地支可以指定要搜索的公元纪年范围,例如"癸丑(2000-2100)"。</p>
<input type="number" id="ce-year" placeholder="请输入公元年">
<input type="text" id="tg-year" placeholder="请输入天干地支">
<button onclick="convert()">转换</button>
<p id="result"></p>
<h4><a href="https://itslittlekevin.com/gadgetslist">»返回工具列表 | Return to Gadgets List«</a></h4>
<p>© itsLittleKevin · All rights reserved.</p>
<script>
// Define the arrays of heavenly stems and earthly branches
const heavenlyStems = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"];
const earthlyBranches = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"];
// Define the function to convert a Common Era year to a 天干地支纪年法 year
function ceToTg(ceYear) {
// Subtract 3 from the Common Era year and get the remainder of dividing by 10 and 12
var remainder10 = (ceYear - 3) % 10;
var remainder12 = (ceYear - 3) % 12;
// Add 10 and 12 to the remainders to get the correct index
remainder10 += 10;
remainder10 -= 1;
remainder10 %= 10;
remainder12 += 12;
remainder12 -= 1;
remainder12 %= 12;
// Get the corresponding heavenly stem and earthly branch from the arrays
var heavenlyStem = heavenlyStems[remainder10];
var earthlyBranch = earthlyBranches[remainder12];
var cycle = Math.floor((ceYear - 3) / 60); // Calculate the cycle number
var offset = cycle * 3; // Calculate the offset of each cycle
// Return the combination of heavenly stem and earthly branch
return heavenlyStem + earthlyBranch + "(" + (ceYear - offset) + "-" + (ceYear + 59 - offset) + ")"; // Add the range specifier
}
// Define the function to convert a 天干地支纪年法 year to a Common Era year
function tgToCe(tgYear) {
// Get the heavenly stem and earthly branch from the 天干地支纪年法 year
var heavenlyStem = tgYear[0];
var earthlyBranch = tgYear[1];
// Get the index of the heavenly stem and earthly branch in the arrays
var index10 = heavenlyStems.indexOf(heavenlyStem);
var index12 = earthlyBranches.indexOf(earthlyBranch);
// Find the smallest Common Era year that has the same remainder of dividing by 10 and 12 as the index
var ceYear = 3;
var cycle = 0;
while ((ceYear % 10 != index10) || (ceYear % 12 != index12)) {
ceYear++;
cycle++;
if (cycle == 60) {
ceYear += 3; // Add 3 years to account for the offset of each cycle
cycle = 0;
}
}
// Return the Common Era year
return ceYear + 4;
}
// Define the function to convert the input year and display the result
function convert() {
// Get the input elements and the result element
var ceYearInput = document.getElementById("ce-year");
var tgYearInput = document.getElementById("tg-year");
var resultElement = document.getElementById("result");
// Get the input values
var ceYear = ceYearInput.value;
var tgYear = tgYearInput.value;
// Check if the input values are valid
if (ceYear && tgYear) {
// Both input values are given, display an error message
resultElement.innerHTML = "Please enter only one input value.";
} else if (ceYear) {
// Only the Common Era year is given, convert it to 天干地支纪年法 year and display the result
var tgYear = ceToTg(ceYear);
resultElement.innerHTML = ceYear + " in Common Era is " + tgYear + " in 天干地支纪年法.";
} else if (tgYear) {
// Only the 天干地支纪年法 year is given, check if it has a range specifier
var range = tgYear.match(/\((\d+)-(\d+)\)/); // Use a regular expression to match the range specifier
if (range) {
// The range specifier is given, get the lower and upper bound of the range
var lower = parseInt(range[1]);
var upper = parseInt(range[2]);
// Remove the range specifier from the 天干地支纪年法 year
tgYear = tgYear.replace(range[0], "");
// Convert the 天干地支纪年法 year to the smallest Common Era year
var ceYear = tgToCe(tgYear);
// Find all the Common Era years that are within the range and are the same 天干地支纪年法 year
var ceYears = [];
while (ceYear <= upper) {
if (ceYear >= lower) {
// The Common Era year is within the range, add it to the array
ceYears.push(ceYear);
}
// Add 60 to the Common Era year to get the next possible Common Era year
ceYear += 60;
}
// Display the result
if (ceYears.length == 0) {
// No Common Era year is found within the range, display an error message
resultElement.innerHTML = "No Common Era year is found for " + tgYear + " in 天干地支纪年法 within the range of " + lower + " to " + upper + ".";
} else if (ceYears.length == 1) {
// Only one Common Era year is found within the range, display the result
resultElement.innerHTML = tgYear + " in 天干地支纪年法 within the range of " + lower + " to " + upper + " is " + ceYears[0] + " in Common Era.";
} else {
// Multiple Common Era years are found within the range, display the result
resultElement.innerHTML = tgYear + " in 天干地支纪年法 within the range of " + lower + " to " + upper + " corresponds to the following Common Era years: " + ceYears.join(", ") + ".";
}
} else {
// The range specifier is not given, convert the 天干地支纪年法 year to the smallest Common Era year and display the result
var ceYear = tgToCe(tgYear);
resultElement.innerHTML = tgYear + " in 天干地支纪年法 is " + ceYear + " in Common Era. Note that there may be other Common Era years that correspond to the same 天干地支纪年法 year. You can specify a range of Common Era years to search for, such as 癸丑(2000-2100).";
}
} else {
// No input value is given, display an error message
resultElement.innerHTML = "请为某一项提供年份";
}
}
</script>
</body>
</html>