Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Days aren't correctly calculated for new visual indicator #35

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 34 additions & 15 deletions src/lib/devices/DeviceCard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,50 @@
let cardEditing = false;

// returns button colour based on time difference
function timeDifference (date: Date) {
function timeDifference(date: Date) {
let currentTime = new Date();
let timeDifference = (Math.round((currentTime.getTime() - date.getTime()) / 1000));
if(timeDifference < 3600) {
return "bg-success"
} else if (timeDifference < 216000) {
return "bg-warning"
let timeDifference = Math.round((currentTime.getTime() - date.getTime()) / 1000);
if (timeDifference < 3600) {
return 'bg-success';
} else if (timeDifference < 86400) {
return 'bg-warning';
}
}

// returns time last seen in human readable format
function timeSince(date: Date) {
let currentTime = new Date();
// gets time difference in seconds
let timeDifference = (Math.round((currentTime.getTime() - date.getTime()) / 1000));

if(timeDifference < 60) {
return `Last seen ${timeDifference} seconds ago`
} else if(timeDifference < 3600) {
return `Last seen ${Math.floor(timeDifference / 60)} minutes ago`
} else if(timeDifference < 216000) {
return `Last seen ${Math.floor(timeDifference / 3600)} hours ago`
let timeDifference = Math.round((currentTime.getTime() - date.getTime()) / 1000);
let timeUnit = '';

if (timeDifference < 60) {
timeUnit = 'seconds';
} else if (timeDifference < 3600) {
timeDifference = Math.floor(timeDifference / 60);
if (timeDifference == 1) {
timeUnit = 'minute';
} else {
timeUnit = 'minutes';
}
return `Last seen ${timeDifference} minutes ago`;
} else if (timeDifference < 86400) {
timeDifference = Math.floor(timeDifference / (60 * 60));
if (timeDifference == 1) {
timeUnit = 'hour';
} else {
timeUnit = 'hours';
}
return `Last seen ${timeDifference} hours ago`;
} else {
return `Last seen ${Math.floor(timeDifference / 216000)} days ago`
timeDifference = Math.floor(timeDifference / (60 * 60 * 24));
if (timeDifference == 1) {
timeUnit = 'day';
} else {
timeUnit = 'days';
}
}
return `Last seen ${timeDifference} ${timeUnit} ago`;
}
</script>

Expand Down