swimtracker-app/SwimTracker/utility/TimeUtils.js

47 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-08-09 21:53:38 +02:00
import moment from 'moment/min/moment-with-locales';
import {i18n} from './i18n';
2020-08-09 21:53:38 +02:00
function timeSince(timeStamp) {
moment.locale(i18n.locale);
2020-08-09 21:53:38 +02:00
const now = Math.floor((new Date()).getTime() / 1000);
const secondsPast = now - timeStamp;
if (secondsPast <= 6 * 3600) {
return moment().seconds(-secondsPast).fromNow();
}
else{
const timeStampDate = new Date(timeStamp * 1000);
const dateNow = new Date();
const timeStampMoment = moment.unix(timeStamp);
let dateStr = "";
if (timeStampDate.getDate() == dateNow.getDate())
dateStr = "Heute, " + timeStampMoment.format("HH:mm");
else if (timeStampDate.getDate() + 1 == dateNow.getDate())
dateStr = "Gestern, " + timeStampMoment.format("HH:mm");
else {
dateStr = timeStampMoment.format("llll");
2020-08-09 21:53:38 +02:00
}
return dateStr;
}
}
const toTimeStr = seconds => {
let minuteStr = String(Math.floor(seconds / 60));
if (minuteStr.length < 2)
minuteStr = "0" + minuteStr;
let secondStr = String(Math.floor(seconds % 60));
if (secondStr.length < 2)
secondStr = "0" + secondStr;
return minuteStr + ":" + secondStr;
}
export { toTimeStr, timeSince };