diff options
| author | Moritz Strohm <strohm@data-quest.de> | 2024-01-29 15:16:24 +0000 |
|---|---|---|
| committer | Moritz Strohm <strohm@data-quest.de> | 2024-01-29 15:16:24 +0000 |
| commit | 7c1df847d94d3956bc763b94b73cebfe108dc9a1 (patch) | |
| tree | e18e003bff65c5bf0748c644d6cd3d235cb1feca /resources/assets/javascripts/lib/datetime.js | |
| parent | da0110d5e85279123e8dde392cb4c926397238bf (diff) | |
StEP 01354, closes #1354
Closes #1354
Merge request studip/studip!2116
Diffstat (limited to 'resources/assets/javascripts/lib/datetime.js')
| -rw-r--r-- | resources/assets/javascripts/lib/datetime.js | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/resources/assets/javascripts/lib/datetime.js b/resources/assets/javascripts/lib/datetime.js new file mode 100644 index 0000000..d58fac8 --- /dev/null +++ b/resources/assets/javascripts/lib/datetime.js @@ -0,0 +1,61 @@ +import { $gettext, $gettextInterpolate } from "./gettext.ts"; + + +const DateTime = { + /** + * A helper method for padding strings with leading zeros. + * @param what The date to pad. + * @param length The length of the string to output. + * @returns {string} A padded version of $what. + */ + pad(what, length = 2) { + return `00000000${what}`.substr(-length); + }, + + /** + * Returns an ISO representation of the specified Date object. + * in the format YYYY-MM-DD. + * + * @param date The Date object to format as ISO date. + * @returns {string} The ISO date string of the Date object. + */ + getISODate(date) { + return date.getFullYear() + '-' + this.pad(date.getMonth() + 1) + '-' + date.getDate(); + }, + + /** + * Returns a formatted version of the specified Date object + * in the Stud.IP date formatting. + * + * @param date The Date object to be formatted. + * @param relative_value Whether to return a relative time value (true) + * or an absolute one (false). Defaults to false. + * @param date_only Whether to return the date only (true) or date and time (false). + * Defaults to false. Only regarded when $relative_value is false. + * @returns {*|string} The date, formatted according to the Stud.IP format for dates. + */ + getStudipDate(date, relative_value = false, date_only = false) { + if (relative_value) { + let now = Date.now(); + if (now - date < 1 * 60 * 1000) { + return $gettext('Jetzt'); + } + if (now - date < 2 * 60 * 60 * 1000) { + return $gettextInterpolate( + $gettext('Vor %{ minutes } Minuten'), + {minutes: Math.floor((now - date) / (1000 * 60))} + ); + } + return this.pad(date.getHours()) + ':' + this.pad(date.getMinutes()); + } + + if (date_only) { + return this.pad(date.getDate()) + '.' + this.pad(date.getMonth() + 1) + '.' + date.getFullYear(); + } + + return this.pad(date.getDate()) + '.' + this.pad(date.getMonth() + 1) + '.' + date.getFullYear() + ' ' + this.pad(date.getHours()) + ':' + this.pad(date.getMinutes()); + } +}; + + +export default DateTime; |
