Skip to content

Instantly share code, notes, and snippets.

@OmanCoding
Created June 3, 2019 07:03
Show Gist options
  • Save OmanCoding/299223f3e2b6e8ab1e13521253e3ecf5 to your computer and use it in GitHub Desktop.
Save OmanCoding/299223f3e2b6e8ab1e13521253e3ecf5 to your computer and use it in GitHub Desktop.
كيف تنتج أوقات "منذ" مثل مواقع التواصل الاجتماعي
function elapsed(time) {
const $SECONDS = Math.abs(Math.floor(Date.now()/1000) - time);
const $iv_table = [
"ثانية","دقيقة", "ساعة", "يوم", "شهر", "سنة",
"ثانيتين","دقيقتين", "ساعتين", "يومين", "شهرين", "سنتين",
"ثواني","دقائق", "ساعات", "أيام", "أشهر", "سنوات"]
const $iv = [$SECONDS, // ثانية
($SECONDS - ($SECONDS%60))/60, // دقيقة
($SECONDS - ($SECONDS%3600))/3600, // ساعة
($SECONDS - ($SECONDS%(3600*24)))/(3600*24), // يوم
($SECONDS - ($SECONDS%(3600*24*30)))/(3600*24*30), // شهر
($SECONDS - ($SECONDS%(3600*24*30*12)))/(3600*24*30*12)]; // سنة
for (let $i = 5; $i >= 0; $i--) {
let $r = $iv[$i];
if ($r > 0) {
if ($r == 1) {
return "منذ" + " " + $iv_table[$i];
}
if ($r == 2) {
$i += 6;
return "منذ" + " " + $iv_table[$i];
}
if ($r > 2) {
if ($r < 11) {
$i += 12;
}
return "منذ" + " " +$r.toString().toArabicDigits() + " " + $iv_table[$i];
}
}
}
}
//https://stackoverflow.com/questions/1675786/convert-from-english-digits-to-arabic-ones-in-html-page
String.prototype.toArabicDigits= function(){
var id= ['۰','۱','۲','۳','٤','٥','٦','٧','۸','۹'];
return this.replace(/[0-9]/g, function(w){
return id[+w]
});
}
// تجربة
console.log(elapsed(1559544760))
console.log(elapsed(1559543822))
console.log(elapsed(1539543822))
console.log(elapsed(1559462673))
console.log(elapsed(1512201229))
console.log(elapsed(1483343629))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment