How to change Gregorian date to Persian date in JavaScript?

前端 未结 9 609
青春惊慌失措
青春惊慌失措 2021-02-04 23:46

Can anyone help me to change Gregorian date to Persian in JavaScript? I want to use it in HTML and JavaScript.

相关标签:
9条回答
  • 2021-02-05 00:14

    To get it in Persian digits:

    new Date().toLocaleDateString('fa-IR');
    // ۱۳۹۹/۱/۲۳
    

    To get it in Latin digits:

    new Date().toLocaleDateString('fa-IR').replace(/([۰-۹])/g, token => String.fromCharCode(token.charCodeAt(0) - 1728));
    // 1399/1/23
    
    0 讨论(0)
  • 2021-02-05 00:15

    You can use the jalaali-js

    Jalaali JavaScript

    A few javascript functions for converting Jalaali (Jalali, Persian, Khayyami, Khorshidi, Shamsi) and Gregorian calendar systems to each other.

    0 讨论(0)
  • 2021-02-05 00:16
    function convertDate(stringArg) {
      const ETF = '۰۱۲۳۴۵۶۷۸۹';
      const ans = g2j(...stringArg.split('/').map(elem => +elem))
        .map(elem =>
          String(elem)
            .split('')
            .map(subElem => ETF[+subElem])
            .join('')
        )
        .join('/');
      return ans;
    }
    
    0 讨论(0)
  • 2021-02-05 00:18

    getPersianDate = (format) => { 
      let week = new Array("يكشنبه", "دوشنبه", "سه شنبه", "چهارشنبه", "پنج شنبه", "جمعه", "شنبه")
      let months = new Array("فروردين", "ارديبهشت", "خرداد", "تير", "مرداد", "شهريور", "مهر", "آبان", "آذر", "دي", "بهمن", "اسفند");
      let today = new Date();
      let d = today.getDay();
      let day = today.getDate();
      let month = today.getMonth() + 1;
      let year = today.getYear();
      year = (window.navigator.userAgent.indexOf('MSIE') > 0) ? year : 1900 + year;
      if (year == 0) {
          year = 2000;
      }
      if (year < 100) {
          year += 1900;
      }
      y = 1;
      for (i = 0; i < 3000; i += 4) {
          if (year == i) {
              y = 2;
          }
      }
      for (i = 1; i < 3000; i += 4) {
          if (year == i) {
              y = 3;
          }
      }
      if (y == 1) {
          year -= ((month < 3) || ((month == 3) && (day < 21))) ? 622 : 621;
          switch (month) {
              case 1:
                  (day < 21) ? (month = 10, day += 10) : (month = 11, day -= 20);
                  break;
              case 2:
                  (day < 20) ? (month = 11, day += 11) : (month = 12, day -= 19);
                  break;
              case 3:
                  (day < 21) ? (month = 12, day += 9) : (month = 1, day -= 20);
                  break;
              case 4:
                  (day < 21) ? (month = 1, day += 11) : (month = 2, day -= 20);
                  break;
              case 5:
              case 6:
                  (day < 22) ? (month -= 3, day += 10) : (month -= 2, day -= 21);
                  break;
              case 7:
              case 8:
              case 9:
                  (day < 23) ? (month -= 3, day += 9) : (month -= 2, day -= 22);
                  break;
              case 10:
                  (day < 23) ? (month = 7, day += 8) : (month = 8, day -= 22);
                  break;
              case 11:
              case 12:
                  (day < 22) ? (month -= 3, day += 9) : (month -= 2, day -= 21);
                  break;
              default:
                  break;
          }
      }
      if (y == 2) {
          year -= ((month < 3) || ((month == 3) && (day < 20))) ? 622 : 621;
          switch (month) {
              case 1:
                  (day < 21) ? (month = 10, day += 10) : (month = 11, day -= 20);
                  break;
              case 2:
                  (day < 20) ? (month = 11, day += 11) : (month = 12, day -= 19);
                  break;
              case 3:
                  (day < 20) ? (month = 12, day += 10) : (month = 1, day -= 19);
                  break;
              case 4:
                  (day < 20) ? (month = 1, day += 12) : (month = 2, day -= 19);
                  break;
              case 5:
                  (day < 21) ? (month = 2, day += 11) : (month = 3, day -= 20);
                  break;
              case 6:
                  (day < 21) ? (month = 3, day += 11) : (month = 4, day -= 20);
                  break;
              case 7:
                  (day < 22) ? (month = 4, day += 10) : (month = 5, day -= 21);
                  break;
              case 8:
                  (day < 22) ? (month = 5, day += 10) : (month = 6, day -= 21);
                  break;
              case 9:
                  (day < 22) ? (month = 6, day += 10) : (month = 7, day -= 21);
                  break;
              case 10:
                  (day < 22) ? (month = 7, day += 9) : (month = 8, day -= 21);
                  break;
              case 11:
                  (day < 21) ? (month = 8, day += 10) : (month = 9, day -= 20);
                  break;
              case 12:
                  (day < 21) ? (month = 9, day += 10) : (month = 10, day -= 20);
                  break;
              default:
                  break;
          }
      }
      if (y == 3) {
          year -= ((month < 3) || ((month == 3) && (day < 21))) ? 622 : 621;
          switch (month) {
              case 1:
                  (day < 20) ? (month = 10, day += 11) : (month = 11, day -= 19);
                  break;
              case 2:
                  (day < 19) ? (month = 11, day += 12) : (month = 12, day -= 18);
                  break;
              case 3:
                  (day < 21) ? (month = 12, day += 10) : (month = 1, day -= 20);
                  break;
              case 4:
                  (day < 21) ? (month = 1, day += 11) : (month = 2, day -= 20);
                  break;
              case 5:
              case 6:
                  (day < 22) ? (month -= 3, day += 10) : (month -= 2, day -= 21);
                  break;
              case 7:
              case 8:
              case 9:
                  (day < 23) ? (month -= 3, day += 9) : (month -= 2, day -= 22);
                  break;
              case 10:
                  (day < 23) ? (month = 7, day += 8) : (month = 8, day -= 22);
                  break;
              case 11:
              case 12:
                  (day < 22) ? (month -= 3, day += 9) : (month -= 2, day -= 21);
                  break;
              default:
                  break;
          }
      }
      if(format===null || format===undefined)
        return `${week[d]} ${day} ${months[month - 1]} ${year}`
      if(format==="y/m/d")
        return `${year}/${month}/${day}`;
      if(format==="d/m/y")
        return `${day}/${month}/${year}`;
    }
    console.log(getPersianDate());
    console.log(getPersianDate("y/m/d"));
    console.log(getPersianDate("d/m/y"));

    0 讨论(0)
  • 2021-02-05 00:19

    You can use jalali-moment as easy as the following code

    import * as moment from 'jalali-moment';
    let persianDate = moment("1989/1/24").locale('fa').format('YYYY/M/D'); // 1367/11/4
    

    demo in plunker

    0 讨论(0)
  • 2021-02-05 00:20

    You can also check this out: JalaliJSCalendar (github)

    JalaliJSCalendar is a JavaScript date-picker and calendar widget based on "Dynarch DHTML Calendar" from Mihai Bazon. The main difference between two is that JalaliJSCalendar supports Persian (AKA Hijri Shamsi) calendar too, which is the official Iranian calendar.

    It contains convertor (jalali.js) and HTML datepicker/calendar

    0 讨论(0)
提交回复
热议问题