Convert UNIX Timestamp To Jalali Calendar

Issue

I want convert unix timestamp date to jalai calendar with jquery , i want set unix timestamp in attribute html tag and then show in tag

example:

1400/03/18 18:35:40

Solution

You can easily use the following code :

Put the unix timestamp value in the attribute

<h1 unix_timestamp="1520482240"></h1>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
        $('[unix_timestamp]').each(function () {
            var unixtimestamp = $(this).attr("unix_timestamp");
            if (unixtimestamp != null) {
                let today = new Date(eval(unixtimestamp * 1000)).toLocaleDateString('fa-IR');
                var date = new Date(unixtimestamp * 1000);
                var hours = date.getHours();
                var minutes = "0" + date.getMinutes();
                var seconds = "0" + date.getSeconds();
                var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
                $(this).html(today + " " + formattedTime);
            }
        });
</script>

You can use it for any tag

Answered By – ZeroToHero

Answer Checked By – Mary Flores (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.