Skip to content

Instantly share code, notes, and snippets.

@hex128
Last active October 25, 2016 01:14
Show Gist options
  • Save hex128/d63000fcc4b6fb2bd4e4 to your computer and use it in GitHub Desktop.
Save hex128/d63000fcc4b6fb2bd4e4 to your computer and use it in GitHub Desktop.
Human readable remaining time till timestamp (Russian)
/**
* @description Get a human readable remaining time till timestamp
* @param till {Number} Timestamp of the moment in the future
* @return {String} Human readable remaining time string
*/
remainingTime = function (till) {
var delta = till - this.timestamp,
string;
if (delta < -60) { // In the past
string = 'Далее';
} else if (delta < 60) { // Less than a minute
string = 'Через несколько секунд';
} else if (delta > 21600) { // More than 6 hours
var localStartTime = this.asObject(till),
localNowTime = this.asObject(),
time = localStartTime.hours.toPaddedString(2) + ':' + localStartTime.minutes.toPaddedString(2);
if (localNowTime.date + 1 == localStartTime.date && localStartTime.hours >= 5) { // After 5 AM next day
string = 'Завтра в ' + time;
} else if (localNowTime.date == localStartTime.date ||
localNowTime.date + 1 == localStartTime.date && localStartTime.hours < 5) {
string = 'В ' + time;
} else {
string = 'Далее';
}
} else {
string = 'Через ';
if (delta >= 3600) { // A hour or more
var hours = Math.floor(delta / 3600);
string += hours == 1 ? 'час' : hours.toString() + ' ' + (hours <= 4 ? 'часа' : 'часов');
} else {
var minutes = Math.floor(delta / 60),
mod = minutes % 10;
string += minutes == 1 ? '' : minutes.toString() + ' ';
if (minutes < 20 && minutes != 1) {
string += minutes <= 4 ? 'минуты' : 'минут';
} else {
string += mod == 1 ? 'минуту' : (mod == 0 || mod > 4 ? 'минут' : 'минуты');
}
}
}
return string;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment