問題描述
我正在開發一個網絡應用程序,并且我有一個 unix 時間戳.我需要使用 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 時間戳創建 Jalali 格式的日期)的可靠且良好的庫嗎?我不需要你的實現或算法,因為這個問題太 bug 并且有很多規則,我需要一個可靠的解決方案.
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(致賈萊)
Another Jalali conversion library https://www.npmjs.com/package/jalaali-js (To Jalai)
使用從 Unix 時間戳轉換的 moment.js 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/Persian 格式?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!