Skip to content

Commit 1584194

Browse files
committed
calendar update
1 parent 4ac46c8 commit 1584194

File tree

3 files changed

+149
-64
lines changed

3 files changed

+149
-64
lines changed

.gitignore

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# C++ objects and libs
2+
*.slo
3+
*.lo
4+
*.o
5+
*.a
6+
*.la
7+
*.lai
8+
*.so
9+
*.dll
10+
*.dylib
11+
12+
# Qt-es
13+
object_script.*.Release
14+
object_script.*.Debug
15+
*_plugin_import.cpp
16+
/.qmake.cache
17+
/.qmake.stash
18+
*.pro.user
19+
*.pro.user.*
20+
*.qbs.user
21+
*.qbs.user.*
22+
*.moc
23+
moc_*.cpp
24+
moc_*.h
25+
qrc_*.cpp
26+
ui_*.h
27+
*.qmlc
28+
*.jsc
29+
Makefile*
30+
*build*
31+
32+
# Qt unit tests
33+
target_wrapper.*
34+
35+
# QtCreator
36+
*.autosave
37+
38+
# QtCreator Qml
39+
*.qmlproject.user
40+
*.qmlproject.user.*
41+
42+
# QtCreator CMake
43+
CMakeLists.txt.user*
44+
45+
# QtCreator 4.8< compilation database
46+
compile_commands.json
47+
48+
# QtCreator local machine specific files for imported projects
49+
*creator.user*

docs/bikram.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
let nepalimonth = saura_masa_num % 12;
9898
this.Year = YearSaka + 135 + Math.floor((saura_masa_num - nepalimonth) / 12);
9999
this.Month = (saura_masa_num + 1) % 12;
100-
this.Day = saura_masa_day;
100+
this.Day = saura_masa_day +1;
101101
}
102102

103103
// Public method to convert Bikram Sambat to Gregorian
@@ -106,7 +106,7 @@
106106
let YearKali = YearSaka + 3179;
107107
let ahar = Math.floor((YearKali * this.YugaCivilDays) / this.YugaRotation_sun);
108108
let [saura_masa_num, saura_masa_day] = this.getSauraMasaDay(ahar);
109-
bsMonth = (bsMonth + 11) % 12; // Adjust the month to the correct range
109+
bsMonth = (bsMonth + 12) % 12; // Adjust the month to the correct range
110110
while (saura_masa_num !== bsMonth || saura_masa_day !== bsDay) {
111111
if (saura_masa_num < bsMonth || (saura_masa_num === bsMonth && saura_masa_day < bsDay)) {
112112
ahar++;
@@ -141,7 +141,7 @@
141141

142142

143143
getDay() {
144-
return this.Day;
144+
return this.Day -1;
145145
}
146146
getNepaliWeekdayName() {
147147
const weekdayNepali = ["आइतबार", "सोमबार", "मङ्गलबार", "बुधबार", "बिहिबार", "शुक्रबार", "शनिबार"];
@@ -176,7 +176,7 @@ getGregorianMonthName() {
176176

177177
// Method to get Nepali day name for Gregorian date
178178
getGregorianDayName(year, month, day) {
179-
let date = new Date(year, month - 1, day);
179+
let date = new Date(year, month, day);
180180
const weekdayNepali = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
181181
return weekdayNepali[date.getDay()];
182182
}

docs/index.html

Lines changed: 96 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
}
4545

4646
.calendar {
47-
display: none; /* hidden untill correct calendar update logic fixed */
47+
4848
width: 100%;
4949
max-width: 600px;
5050
border: 1px solid #ccc;
@@ -232,19 +232,17 @@ <h1 class="text-center">Nepali Date Converter</h1>
232232
<center>
233233
<p>Converted Date: <span id="gYearResult" class="result"></span> <span id="gMonthResult"
234234
class="result"></span> <span
235-
id="gDayResult" class="result"></span> <!--<span id="gregorianWeekday"
236-
class="result"></span> removed --></p></center>
235+
id="gDayResult" class="result"></span></p></center>
237236
</div>
238237
</div>
239238
</div>
240239
</div>
241240

242241
<div class="container">
243242
<div class="calendar" id="bikramCalendar">
244-
<h3>Bikram Sambat Calendar Current Month </h3>
245-
<div>
246-
<span id="currentDate"></span>
247-
</div>
243+
<h3>Bikram Sambat Calendar</h3>
244+
245+
<br>
248246
<table>
249247
<thead>
250248
<tr>
@@ -264,67 +262,105 @@ <h3>Bikram Sambat Calendar Current Month </h3>
264262
</div>
265263
</div>
266264
<script>
267-
document.addEventListener('DOMContentLoaded', function () {
268-
const bikram = new Bikram();
269-
270-
function generateBikramCalendar(bsYear, bsMonth) {
271-
const daysInMonth = bikram.daysInMonth(bsYear, bsMonth);
272-
const firstDay = bikram.getDayOfWeek(bsYear, bsMonth, 1);
273-
const currentDay = new Date().getDay();
274-
const bikramBody = document.getElementById('bikramBody');
275-
bikramBody.innerHTML = '';
276-
277-
for (let i = 0; i < 6; i++) {
278-
let row = document.createElement('tr');
279-
for (let j = 0; j < 7; j++) {
280-
let cell = document.createElement('td');
281-
let dayNumber = i * 7 + j - firstDay;
282-
283-
if (dayNumber > 0 && dayNumber <= daysInMonth) {
284-
cell.innerHTML = convertToNepaliDigits(dayNumber);
285-
286-
// Convert Bikram Sambat date to Gregorian date
287-
let gDate = bikram.toGregorian(bsYear, bsMonth, dayNumber);
288-
289-
// Calculate Tithi
290-
try {
291-
let tithiInfo = calculateTithi(gDate.year, gDate.month, gDate.day);
292-
if (tithiInfo && tithiInfo.tithi) {
293-
cell.setAttribute('title', `${tithiInfo.tithi} - ${tithiInfo.paksha}`);;
294-
cell.innerHTML += `<br><span class="tithi-label">${tithiInfo.tithi}</span>`;
295-
} else {
296-
console.error(`Tithi info is undefined for: Year: ${gDate.year}, Month: ${gDate.month}, Day: ${gDate.day}`);
297-
}
298-
} catch (error) {
299-
console.error(`Error calculating tithi for: Year: ${gDate.year}, Month: ${gDate.month}, Day: ${gDate.day}`, error);
300-
}
301-
302-
if (j === currentDay && i === 0) {
303-
cell.style.color = 'red';
304-
}
305-
} else {
306-
cell.textContent = '';
265+
document.addEventListener('DOMContentLoaded', function () {
266+
const bikram = new Bikram();
267+
268+
// Function to generate the Bikram Sambat calendar
269+
function generateBikramCalendar(bsYear, bsMonth) {
270+
const daysInMonth = bikram.daysInMonth(bsYear, bsMonth); // Total number of days in the selected month
271+
const firstDay = bikram.getDayOfWeek(bsYear, bsMonth, 1); // First day of the month (Bikram Sambat day of the week)
272+
273+
// Get today's Bikram Sambat date
274+
const today = new Date();
275+
const todayBikramDate = bikram.fromGregorian(today.getFullYear(), today.getMonth() + 1, today.getDate());
276+
277+
278+
const bikramBody = document.getElementById('bikramBody');
279+
bikramBody.innerHTML = ''; // Clear the existing calendar body
280+
281+
let dayCounter = 1; // Start with the 1st day of the month
282+
let row = document.createElement('tr'); // Create the first row for weekdays
283+
284+
// Loop through the weeks (6 rows to handle all days in the month)
285+
for (let i = 0; i < 6; i++) {
286+
// Loop through the days of the week (7 columns)
287+
for (let j = 0; j < 7; j++) {
288+
let cell = document.createElement('td');
289+
290+
// Check if it's the correct day in the calendar
291+
if (i === 0 && j < firstDay) {
292+
// Fill empty cells before the first day of the month
293+
cell.textContent = '';
294+
} else if (dayCounter <= daysInMonth) {
295+
// Fill cells with actual days
296+
cell.innerHTML = convertToNepaliDigits(dayCounter);
297+
298+
// Convert Bikram Sambat date to Gregorian date
299+
let gDate = bikram.toGregorian(bsYear, bsMonth, dayCounter);
300+
301+
// Calculate Tithi (if available)
302+
try {
303+
let tithiInfo = calculateTithi(gDate.year, gDate.month, gDate.day);
304+
if (tithiInfo && tithiInfo.tithi) {
305+
cell.setAttribute('title', `${tithiInfo.tithi} - ${tithiInfo.paksha}`);
306+
cell.innerHTML += `<br><span class="tithi-label">${tithiInfo.tithi}</span>`;
307307
}
308+
} catch (error) {
309+
console.error(`Error calculating tithi for: Year: ${gDate.year}, Month: ${gDate.month}, Day: ${gDate.day}`, error);
310+
}
308311

309-
row.appendChild(cell);
312+
// Highlight today's Bikram Sambat date
313+
if (dayCounter === todayBikramDate) {
314+
cell.style.backgroundColor = 'yellow'; // Highlight today
310315
}
311-
bikramBody.appendChild(row);
316+
317+
dayCounter++; // Increment the day counter for the next day
318+
} else {
319+
// Fill remaining cells after the last day of the month
320+
cell.textContent = '';
312321
}
313322

314-
document.getElementById('currentDate').textContent = `${bikram.getBikramMonthName(bsMonth)} ${convertToNepaliDigits(bsYear)}`;
323+
row.appendChild(cell); // Append the cell to the row
315324
}
316325

317-
const today = new Date();
318-
bikram.fromGregorian(today.getFullYear(), today.getMonth() + 1, 1);
319-
generateBikramCalendar(bikram.getYear(), bikram.getMonth() + 1);
326+
bikramBody.appendChild(row); // Append the row to the calendar body
327+
328+
// If dayCounter exceeds the number of days in the month, stop adding new rows
329+
if (dayCounter > daysInMonth) {
330+
break;
331+
}
332+
333+
// Create a new row for the next week
334+
row = document.createElement('tr');
335+
}
336+
337+
}
338+
339+
340+
// Function to update the calendar from selected Gregorian date
341+
function updateCalendarFromGregorian() {
342+
const gYear = parseInt(document.getElementById('gYear').value);
343+
const gMonth = parseInt(document.getElementById('gMonth').value);
344+
const gDay = parseInt(document.getElementById('gDay').value);
345+
346+
// Convert the Gregorian date to Bikram Sambat date
347+
bikram.fromGregorian(gYear, gMonth, gDay);
348+
349+
// Generate the calendar for the corresponding Bikram Sambat year and month
350+
generateBikramCalendar(bikram.getYear(), bikram.getMonth() + 1); // Bikram months are 1-indexed
351+
}
352+
353+
// Listen for changes in the Gregorian date dropdowns
354+
document.getElementById('gYear').addEventListener('change', updateCalendarFromGregorian);
355+
document.getElementById('gMonth').addEventListener('change', updateCalendarFromGregorian);
356+
//document.getElementById('gDay').addEventListener('change', updateCalendarFromGregorian);
357+
358+
// Generate the initial Bikram Sambat calendar
359+
const today = new Date();
360+
bikram.fromGregorian(today.getFullYear(), today.getMonth() + 1, 1); // Initialize Bikram from current Gregorian date
361+
generateBikramCalendar(bikram.getYear(), bikram.getMonth() + 1); // Generate the calendar for the current month
362+
});
320363

321-
document.getElementById('bikramCalendar').addEventListener('change', function () {
322-
const bsYear = parseInt(document.getElementById('bikramYear').value);
323-
const bsMonth = parseInt(document.getElementById('bikramMonth').value);
324-
generateBikramCalendar(bsYear, bsMonth);
325-
bikram.fromNepali(bsYear, bsMonth, 1);
326-
});
327-
});
328364
</script>
329365

330366

0 commit comments

Comments
 (0)