-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
104 lines (90 loc) · 3.73 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
for (let year = 1971; year <= 2100; year++) {
document.getElementById('bsYear').innerHTML += `<option value="${year}">${year} BS</option>`;
}
for (let year = 1914; year <= 2044; year++) {
document.getElementById('adYear').innerHTML += `<option value="${year}">${year} AD</option>`;
}
function clearResults() {
document.getElementById('result1').textContent = '';
document.getElementById('error1').textContent = '';
document.getElementById('result2').textContent = '';
document.getElementById('error2').textContent = '';
}
function toggleConversionSection() {
clearResults();
const conversionType = document.getElementById('conversionType').value;
const bsToADSection = document.getElementById('bsToADSection');
const adToBSSection = document.getElementById('adToBSSection');
if (conversionType === 'BStoAD') {
bsToADSection.style.display = 'block';
adToBSSection.style.display = 'none';
} else if (conversionType === 'ADtoBS') {
adToBSSection.style.display = 'block';
bsToADSection.style.display = 'none';
} else {
bsToADSection.style.display = 'none';
adToBSSection.style.display = 'none';
}
}
async function convertBSToAD() {
clearResults();
const year = document.getElementById('bsYear').value;
const month = document.getElementById('bsMonth').value;
const day = document.getElementById('bsDay').value;
if (!year || !month || !day) {
document.getElementById('error1').textContent = 'Please fill in all fields.';
return;
}
try {
const response = await fetch(`https://sudhang.pythonanywhere.com/BStoAD/${year}/${month}/${day}`);
const data = await response.json();
if (response.ok) {
const formattedADDate = `${data.ad_date.day}, ${getMonthName(data.ad_date.month)} ${data.ad_date.year}`;
document.getElementById('result1').textContent = formattedADDate;
} else {
document.getElementById('error1').textContent = data.error || 'An error occurred.';
}
} catch (error) {
document.getElementById('error1').textContent = 'Error fetching the date. Please try again.';
console.error('Fetch error:', error);
}
}
async function convertADToBS() {
clearResults();
const year = document.getElementById('adYear').value;
const month = document.getElementById('adMonth').value;
const day = document.getElementById('adDay').value;
if (!year || !month || !day) {
document.getElementById('error2').textContent = 'Please fill in all fields.';
return;
}
try {
const response = await fetch(`https://sudhang.pythonanywhere.com/ADtoBS/${year}/${month}/${day}`);
const data = await response.json();
if (response.ok) {
const formattedBSDate = `${data.bs_date.day} ${getBSMonthName(data.bs_date.month)} ${data.bs_date.year}`;
document.getElementById('result2').textContent = formattedBSDate;
} else {
document.getElementById('error2').textContent = data.error || 'An error occurred.';
}
} catch (error) {
document.getElementById('error2').textContent = 'Error fetching the date. Please try again.';
console.error('Fetch error:', error);
}
}
function getMonthName(month) {
const monthNames = [
"",
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
return monthNames[month];
}
function getBSMonthName(month) {
const bsMonthNames = [
"",
"Baishak", "Jestha", "Ashadh", "Shrawan", "Bhadra",
"Ashwin", "Kartik", "Mangsir", "Poush", "Magh", "Falgun", "Chaitra"
];
return bsMonthNames[month];
}