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 }  ; 
295-               }  else  { 
296-                 console . error ( `Tithi info is undefined for: Year: ${ gDate . year } ${ gDate . month } ${ gDate . day }  ) ; 
297-               } 
298-             }  catch  ( error )  { 
299-               console . error ( `Error calculating tithi for: Year: ${ gDate . year } ${ gDate . month } ${ 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 }  ; 
307307          } 
308+         }  catch  ( error )  { 
309+           console . error ( `Error calculating tithi for: Year: ${ gDate . year } ${ gDate . month } ${ 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