skip to Main Content

Using Intl.DateTimeFormat() as shown below:

const fullHindi = {
    weekday: 'long',
    dayPeriod: 'long',
    year: 'numeric',
    month: 'short',
    day: 'numeric',
    dayPeriod: "narrow",
    hour: '2-digit',
    minute: '2-digit',
    second: 'numeric',
    calendar: 'indian',
    timeZone: 'Asia/Kolkata',
    numberingSystem: 'deva',
    formatMatcher: 'basic',
    hourCycle: 'h24',
};
const samay = new Intl.DateTimeFormat("hi", fullHindi);
const today = new Date();
const hindi = samay.format(today);
console.log({hindi}); // शक मंगलवार, २२ श्रावण १९४६, २३:४४:०८

but it’s not correct though its Shuka/Saptami शुक्ल सप्तमी

In a nutshell, the lunar calendar which have 15 days and paksha so requirement is that it changes to different paksha ( either Shukla/Krishna ) see more details here

My question: is there any format or style option configuration is there which change this; similar to h12 and h24 format for time change ot to AM PM

2

Answers


  1. I Think this is the output you expext

    const getPaksha = (date) => {
      const day = date.getDate();
      return day <= 15 ? 'Shukla' : 'Krishna';
    };
    
    const getTithi = (date) => {
      const day = date.getDate();
      return `Tithi ${day}`; 
    };
    
    const getHinduMonthAndYear = (date) => {
      const months = [
        'Chaitra', 'Vaishakha', 'Jyeshtha', 'Ashadha', 'Shravana', 'Bhadrapada',
        'Ashvin', 'Kartika', 'Margashirsha', 'Pushya', 'Magha', 'Phalguna'
      ];
      const monthIndex = (date.getMonth() + 1) % 12; 
      return { month: months[monthIndex], year: date.getFullYear() };
    };
    
    const formatHinduDate = (date) => {
      const paksha = getPaksha(date);
      const tithi = getTithi(date);
      const { month, year } = getHinduMonthAndYear(date);
      
      return `${paksha} Paksha, ${tithi}, ${month}, ${year}`;
    };
    
    const today = new Date();
    const formattedDate = formatHinduDate(today);
    console.log(formattedDate);
    Login or Signup to reply.
  2. function getPaksha(date) {
        const day = date.getDate();
        if (day <= 15) {
            return "शुक्ल";
        } else {
            return "कृष्ण";
        }
    }
    
    function formatDateWithPaksha(date) {
        const paksha = getPaksha(date);
    
        const fullHindi = {
            weekday: 'long',
            year: 'numeric',
            month: 'short',
            day: 'numeric',
            hour: '2-digit',
            minute: '2-digit',
            second: 'numeric',
            calendar: 'indian',
            timeZone: 'Asia/Kolkata',
            numberingSystem: 'deva',
            hourCycle: 'h24',
        };
    
        const samay = new Intl.DateTimeFormat("hi-IN", fullHindi);
        const formattedDate = samay.format(date);
    
        return `${paksha} पक्ष, ${formattedDate}`;
    }
    
    const today = new Date();
    const hindiDateWithPaksha = formatDateWithPaksha(today);
    console.log({ hindiDateWithPaksha });

    Uses Intl.DateTimeFormat to format the date in Hindi, including weekday, year, month, day, and time.

    Adds the Paksha (from getPaksha) to the formatted date string.
    The current date is formatted and displayed with the Paksha included.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search