你如何将Unix时间戳转换为Jalali / Shamsi /波斯语格式? [英] How do you convert a Unix timestamp to Jalali/Shamsi/Persian format?
问题描述
我正在开发一个Web应用程序,我有一个unix timeStamp。
我需要使用jQuery选择器将unix日期格式转换为Jalali / Persian / Shamsi日历,然后使用javascript库进行转换。
类似以下代码转换 Unix-Date
到 Jalali-Date
:
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>
我搜索过但没有找到任何好的库。你知道一个可靠而好的库来转换(或者从unix timeStamp创建Jalali格式的日期)。我不需要你的实现或算法,因为这个问题太麻烦而且有很多规则,我需要一个可靠的解决方案。
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.
谢谢
推荐答案
我建议使用 moment.js
( https://momentjs.com/ )这是一个可靠的JavaScript时间库,允许您在JavaScript中格式化时间戳。下面是一个如何解析时间戳并将其格式化为您想要的任何内容的示例。
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.
这与 https://www.npmjs.com/package/jalali-date结合使用将为您提供jalali日期。
This in conjunction with https://www.npmjs.com/package/jalali-date will get you your jalali date.
这里还有一个波斯语的moment.js扩展 https://www.npmjs.com/package/moment-jalaali (从时刻到Jalali)
There is a moment.js extension for Persian here also https://www.npmjs.com/package/moment-jalaali (From moment to Jalali)
另一个Jalali转换库 https://www.npmjs.com/package/jalaali -js (致Jalai)
Another Jalali conversion library https://www.npmjs.com/package/jalaali-js (To Jalai)
使用moment.js从Unix时间戳转换Jalali的示例小提示 https://jsfiddle.net/uw82ozpd/9/
An example fiddle using moment.js Jalali conversion from Unix Timestamp https://jsfiddle.net/uw82ozpd/9/
带注释的相关代码段:
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);
});
这篇关于你如何将Unix时间戳转换为Jalali / Shamsi /波斯语格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!