Commit 1140b802 authored by Konrad Mohrfeldt's avatar Konrad Mohrfeldt
Browse files

refactor: clean up time & date functions

* replace all `var` declarations and use `const` where appropriate
* strip unnecessary temporary variables
* replace single character variable names
* replace multiline variable declarations
* simplify expressions
* replace invalid uses of parseInt
parent 771415e2
Pipeline #1847 passed with stage
in 1 minute and 39 seconds
var day = [
const day = [
'rrule.day.sunday',
'rrule.day.monday',
'rrule.day.tuesday',
......@@ -19,23 +19,21 @@ export default {
},
apiDate: function (date) {
var d = new Date(date)
return d.getFullYear() + '-' + this.leadingZero(d.getMonth() + 1) + '-' + this.leadingZero(d.getDate())
const dateObj = new Date(date)
return `${dateObj.getFullYear()}-${this.leadingZero(dateObj.getMonth() + 1)}-${this.leadingZero(dateObj.getDate())}`
},
getWeekdayFromApiDate: function (date) {
let d = new Date(date)
let w = d.getDay()
const dateObj = new Date(date)
const weekday = dateObj.getDay()
// Date.getDay() returns 0 for Sundays, but for our APIs we need 0 for Mondays
if (w === 0) {
if (weekday === 0) {
return 6
} else {
return w - 1
return weekday - 1
}
},
prettyDate: function (date) {
var d = new Date(date)
return d.toLocaleString(
return new Date(date).toLocaleString(
this.$activeLocale(),
{
weekday: 'long',
......@@ -47,9 +45,7 @@ export default {
},
prettyTime: function(date) {
var d = new Date(date)
return d.toLocaleString(
return new Date(date).toLocaleString(
this.$activeLocale(),
{
hour: '2-digit',
......@@ -59,9 +55,7 @@ export default {
},
prettyDateTime: function (date) {
var d = new Date(date)
return d.toLocaleString(
return new Date(date).toLocaleString(
this.$activeLocale(),
{
weekday: 'long',
......@@ -77,9 +71,9 @@ export default {
return hours.replace(/:00$/, '')
},
prettyDuration: function (start, end) {
var seconds = (new Date(end).getTime() - new Date(start).getTime()) / 1000
var minutes = Math.floor(seconds / 60)
var duration = minutes + 'min'
const seconds = (new Date(end).getTime() - new Date(start).getTime()) / 1000
const minutes = Math.floor(seconds / 60)
let duration = minutes + 'min'
if (minutes < seconds / 60) {
duration += ' '
duration += seconds - 60 * minutes
......@@ -95,9 +89,9 @@ export default {
prettyNanoseconds: function (ns) {
const durationInMilliseconds = Math.floor(ns / 1000 / 1000)
const seconds = parseInt((durationInMilliseconds / 1000) % 60),
minutes = parseInt((durationInMilliseconds / (1000 * 60)) % 60),
hours = parseInt((durationInMilliseconds / (1000 * 60 * 60)) % 24)
const seconds = Math.floor((durationInMilliseconds / 1000) % 60)
const minutes = Math.floor((durationInMilliseconds / (1000 * 60)) % 60)
const hours = Math.floor((durationInMilliseconds / (1000 * 60 * 60)) % 24)
return this.leadingZero(hours) + ':' + this.leadingZero(minutes) + ':' + this.leadingZero(seconds)
},
......@@ -114,21 +108,13 @@ export default {
return seconds * 1000 * 1000 * 1000
},
minutesToHm: function(minutes) {
var m = minutes % 60
var h = (minutes-m)/60
const numMinutes = minutes % 60
const numHours = (minutes - numMinutes) / 60
return `${this.leadingZero(h)}:${this.leadingZero(m)}`
return `${this.leadingZero(numHours)}:${this.leadingZero(numMinutes)}`
},
prettyWeekday: function (weekday) {
let w
if (weekday === 6) {
w = 0
} else {
w = weekday + 1
}
return this.$t(day[w])
return this.$t(day[(weekday + 1) % 7])
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment