Search code examples
javascriptdatemomentjs

convert date shamsi to miladi in js


I have a date in this format 1379/02/10 Now I want to turn this date into AD That is, the date 2000/4/29

I also use the momentJs library Please help me if possible by using this library Thank you for your time


Solution

  • You can use jalaali-js or moment-jalaali or jalali-date (via) or Ali Amini's or Mohsen Alyafei's solution. Or draw inspiration from related questions. People not knowing Persian culture can get confused by the multiple names for the calendar - Jalali, Persian, Khorshidi, Shamsi. Maybe you can elaborate the differences a bit?

    Here is a modern solution using the Skypack CDN for easy npm package access in the browser. Just remember to always declare your variables with const/let/var and no global ones inside es6-modules:

    <script type="module">
    import jalaali from 'https://cdn.skypack.dev/jalaali-js'
    import jMoment from 'https://cdn.skypack.dev/moment-jalaali'
    
    
    // jalaali-js
    let g = jalaali.toGregorian(1379, 2, 10);
    let j = jalaali.toJalaali(new Date(2000, 4-1, 29)) // january is month 0
    
    // moment-jalaali
    let moment = jMoment('1379/2/10', 'jYYYY/jM/jD') // Parse a Jalaali/Shamsi date
    let gregorian = moment.format('jYYYY/jM/jD [is] YYYY/M/D')
    
    
    console.log('jalaali-js version:',g,j,'moment-jalaali version: ', gregorian)
    </script>