How to change Gregorian date to Persian date in JavaScript?

前端 未结 9 610
青春惊慌失措
青春惊慌失措 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
    

提交回复
热议问题