12

I'm developing a web App and I have a unix timeStamp. I need to convert a unix date format to Jalali/Persian/Shamsi Calendar by using jQuery selectors and then convert it by using javascript library.
Something Like below code to convert Unix-Date to Jalali-Date:

<html>
  <head>
  <meta charset="utf-8">
  </head>

  <body>
  <div class="Unix-Date">1494259627</div> <!-- Unix equal of 1396/2/18 -->
  <div class="Jalali-Date"></div>

  <script src="jquery.js"></script>
  <script src="external-library.js"></script>
  <script>
  $(document).ready(function() {
  var UnixValue;
  var JalaliValue;
  UnixValue = $(".Unix-Date").html();
  JalaliValue = new JalaliExternalFunction(UnixValue);
  $(".Jalali-Date").text(JalaliValue);
  });
  </script>
  </body>
</html>

I searched but didn't found any good library. Do you know a reliable and good library for converting (or creating dates in Jalali format from a unix timeStamp). I don't need your implementation or an algorithm, cause this issue is too buggy and has a lot of rules, I need a reliable solution.

Thank

Mojtaba Reyhani
  • 447
  • 1
  • 6
  • 19

3 Answers3

12

I would suggest using moment.js (https://momentjs.com/) which is reliable JavaScript Time library that allows you to format your timestamp in JavaScript. Below is an example of how you can parse a timestamp and format it to whatever you want using it.

//formatting Unix timestamp.
var date = moment.unix(value).format("MM/DD/YYYY");

You also tagged localization which can be done by using;

 var localeDate = moment(date).locale("LT");

More examples can be found on there website.

This in conjunction with https://www.npmjs.com/package/jalali-date will get you your jalali date.

There is a moment.js extension for Persian here also https://www.npmjs.com/package/moment-jalaali (From moment to Jalali)

Another Jalali conversion library https://www.npmjs.com/package/jalaali-js (To Jalai)

An example fiddle using moment.js Jalali conversion from Unix Timestamp https://jsfiddle.net/uw82ozpd/9/

Relevant code snippet with comments:

var UnixValue;
var JalaliValue;

$(document).ready(function() {

//get the Unix Date from HTML
var UnixValue = $(".Unix-Date").html();

//Get a moment timestamp in the format simmilar to our next conversion
var date = moment.unix(UnixValue).format("MM/DD/YY");

//Convert from normal moment to our jalali moment exstension using j's as below
var JalaliValue = moment(date).format('jYYYY/jM/jD');

$(".Jalali-Date").text(JalaliValue);

});
li x
  • 3,953
  • 2
  • 31
  • 51
  • Thank you so much for your help and reply, But it seems that `moment.js` only translate day and mount name to persian and the date still remain in gregorian. – Mojtaba Reyhani May 08 '17 at 15:01
  • Turns out there is also a moment extension for Persian, updating answer. – li x May 08 '17 at 15:05
  • Going to keep adding librarys that I find that could be useful, let me know if any of them work for you! – li x May 08 '17 at 15:08
  • What exatly would the format look like you want? In Persian writing or? – li x May 08 '17 at 15:44
  • https://jsfiddle.net/uw82ozpd/9/ Here is the fiddle so far with it converting to jalali date. – li x May 08 '17 at 15:46
  • My pleasure @MojtabaReyhani – li x May 08 '17 at 15:54
  • 1
    It's likely that the very large script I inserted into the html caused it to be removed from jsfiddle, The code for moment jilali can be found here https://github.com/jalaali/moment-jalaali/blob/master/build/moment-jalaali.js copy that and save it in your directory and then use `` (make sure to save the JS file as `jalali-js`'') Afterwards follow the instructions on my answer :-) – li x May 08 '17 at 17:51
  • 1
    You'll also need to include the base moment js which you can do by including `` within your html. – li x May 08 '17 at 17:53
  • Thank you so much, I appreciate you very much for everything. – Mojtaba Reyhani May 08 '17 at 22:11
7

you can use toLocalDateString()

let mydate = new Date(timestamp);
let mypersiandate = mydate.toLocaleDateString('fa-IR');
console.log(mypersiandate);
Ali_Hr
  • 4,017
  • 3
  • 27
  • 34
4

I have these simple functions:

function convertTimeStampToJalali(timestamp){
    var date = new Date(timestamp);
    if(!date)
        return false;
    return ( gregorian_to_jalali(date.getFullYear(),(date.getMonth()+1),date.getDate()) );
}//end of function convertTimeStampToJalali

function gregorian_to_jalali(gy,gm,gd){
    g_d_m=[0,31,59,90,120,151,181,212,243,273,304,334];
    if(gy > 1600){
        jy=979;
        gy-=1600;
    }else{
        jy=0;
        gy-=621;
    }
    gy2=(gm > 2)?(gy+1):gy;
    days=(365*gy) +(parseInt((gy2+3)/4)) -(parseInt((gy2+99)/100)) +(parseInt((gy2+399)/400)) -80 +gd +g_d_m[gm-1];
    jy+=33*(parseInt(days/12053)); 
    days%=12053;
    jy+=4*(parseInt(days/1461));
    days%=1461;
    if(days > 365){
        jy+=parseInt((days-1)/365);
        days=(days-1)%365;
    }
    jm=(days < 186)?1+parseInt(days/31):7+parseInt((days-186)/30);
    jd=1+((days < 186)?(days%31):((days-186)%30));
    return [jy,jm,jd];
}//end of function gregorian_to_jalali
Abolfazl Miadian
  • 3,099
  • 2
  • 19
  • 15