問題描述
我正在開發(fā)一個(gè)網(wǎng)絡(luò)應(yīng)用程序,并且我有一個(gè) unix 時(shí)間戳.我需要使用 jQuery 選擇器將 unix 日期格式轉(zhuǎn)換為 Jalali/Persian/Shamsi 日歷,然后使用 javascript 庫進(jìn)行轉(zhuǎn)換.
下面的代碼將 Unix-Date
轉(zhuǎn)換為 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>
我搜索但沒有找到任何好的圖書館.您知道用于轉(zhuǎn)換(或從 unix 時(shí)間戳創(chuàng)建 Jalali 格式的日期)的可靠且良好的庫嗎?我不需要你的實(shí)現(xiàn)或算法,因?yàn)檫@個(gè)問題太 bug 并且有很多規(guī)則,我需要一個(gè)可靠的解決方案.
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/),這是一個(gè)可靠的 JavaScript 時(shí)間庫,允許您在 JavaScript 中格式化您的時(shí)間戳.下面是一個(gè)示例,說明如何解析時(shí)間戳并將其格式化為您想要使用的任何格式.
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");
您還標(biāo)記了可以通過使用來完成的本地化;
You also tagged localization which can be done by using;
var localeDate = moment(date).locale("LT");
更多示例可以在該網(wǎng)站上找到.
More examples can be found on there website.
這與 https://www.npmjs.com/package/jalali-date 會(huì)給你你的 jalali 日期.
This in conjunction with https://www.npmjs.com/package/jalali-date will get you your jalali date.
這里還有一個(gè)波斯語的 moment.js 擴(kuò)展 https://www.npmjs.com/package/moment-jalaali(從時(shí)刻到 Jalali)
There is a moment.js extension for Persian here also https://www.npmjs.com/package/moment-jalaali (From moment to Jalali)
另一個(gè) Jalali 轉(zhuǎn)換庫 https://www.npmjs.com/package/jalaali-js(致賈萊)
Another Jalali conversion library https://www.npmjs.com/package/jalaali-js (To Jalai)
使用從 Unix 時(shí)間戳轉(zhuǎn)換的 moment.js Jalali 的示例小提琴 https://jsfiddle.net/uw82ozpd/9/
An example fiddle using moment.js Jalali conversion from Unix Timestamp https://jsfiddle.net/uw82ozpd/9/
帶注釋的相關(guān)代碼段:
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);
});
這篇關(guān)于如何將 Unix 時(shí)間戳轉(zhuǎn)換為 Jalali/Shamsi/Persian 格式?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!