mirror of
https://github.com/FH-Complete/FHC-Core.git
synced 2026-06-26 00:19:28 +00:00
feature(DateHelpers.js): creates a js helper file to create, handle and manipulate js dates
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import BsModal from "../../Bootstrap/Modal.js";
|
||||
import Alert from "../../Bootstrap/Alert.js";
|
||||
import LvMenu from "./LvMenu.js"
|
||||
import { numberPadding,formatDate } from "../../../helpers/DateHelpers.js"
|
||||
|
||||
export default {
|
||||
components: {
|
||||
@@ -40,32 +41,24 @@ export default {
|
||||
if (!this.event.start instanceof Date){
|
||||
return this.event.start;
|
||||
}
|
||||
return this.event.start.getHours() + ":" + this.event.start.getMinutes();
|
||||
return numberPadding(this.event.start.getHours()) + ":" + numberPadding(this.event.start.getMinutes());
|
||||
},
|
||||
end_time: function(){
|
||||
if (!this.event.end) return 'N/A';
|
||||
if (!this.event.end instanceof Date) {
|
||||
return this.event.end;
|
||||
}
|
||||
return this.event.end.getHours() + ":" +
|
||||
// returns the string '00' if the function getMinutes returns 0
|
||||
(this.event.end.getMinutes() || typeof this.event.end.getMinutes() === 'number' && this.event.end.getMinutes().toString() + '0');
|
||||
return numberPadding(this.event.end.getHours()) + ":" + numberPadding(this.event.end.getMinutes());
|
||||
}
|
||||
},
|
||||
methods:{
|
||||
add_padding_to_date_number: function(number)
|
||||
mehtodNumberPadding: function(number)
|
||||
{
|
||||
return number.toString().length == 1 ? '0' + number.toString() : number.toString();
|
||||
return numberPadding(number);
|
||||
},
|
||||
format_date: function(d)
|
||||
methodFormatDate: function(d)
|
||||
{
|
||||
let date = new Date(d);
|
||||
// if the date is an invalid string then creating a date from the string will fail and N/A is returned
|
||||
if (isNaN(date.valueOf()))
|
||||
{
|
||||
return 'N/A';
|
||||
}
|
||||
return `${this.add_padding_to_date_number(date.getDate())}.${this.add_padding_to_date_number(date.getMonth() + 1)}.${this.add_padding_to_date_number(date.getFullYear())}`;
|
||||
return formatDate(d);
|
||||
},
|
||||
onModalShow: function()
|
||||
{
|
||||
@@ -107,7 +100,7 @@ export default {
|
||||
$p.t('global','datum')+':'
|
||||
:''
|
||||
}}</th>
|
||||
<td>{{format_date(event.datum)}}</td>
|
||||
<td>{{methodFormatDate(event.datum)}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>{{
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
|
||||
// HELPER FILE -- that contains multiple functions which create, handle and manipulate js dates
|
||||
|
||||
// custom Error class for DateHelpers.js misuse
|
||||
class DateHelperError extends Error {
|
||||
constructor(message) {
|
||||
super(message);
|
||||
this.name = "DateHelperError";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* adds padding to a number
|
||||
*
|
||||
* @param {number|string} number - the number on which padding should be added.
|
||||
* @returns {string} number with padding.
|
||||
*/
|
||||
export function numberPadding(number) {
|
||||
if(typeof number !== "string" && typeof number !== "number")
|
||||
{
|
||||
throw new TypeError("function numberPadding in file DateHelpers.js is only usable with strings or numbers");
|
||||
}
|
||||
if(number.toString().length > 2)
|
||||
{
|
||||
throw new DateHelperError("The number on which the padding should be added should not be longer than to 2 characters, please refere to the function numberPadding in the helper file DateHelpers.js");
|
||||
}
|
||||
return number.toString().length == 1 ? '0' + number.toString() : number.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* formats date to dd.mm.yyyy
|
||||
*
|
||||
* @param {string|Date} d - the date that should be formatted.
|
||||
* @returns {string} formatted date string.
|
||||
*/
|
||||
export function formatDate(d) {
|
||||
// parameter is of type Date
|
||||
if(d instanceof Date)
|
||||
{
|
||||
if (isNaN(date.valueOf())) {
|
||||
return 'N/A';
|
||||
}
|
||||
// if the date is an invalid string then creating a date from the string will fail and N/A is returned
|
||||
return `${numberPadding(d.getDate())}.${numberPadding(d.getMonth() + 1)}.${d.getFullYear()}`;
|
||||
}
|
||||
// parameter is of type string
|
||||
else if (typeof d === "string")
|
||||
{
|
||||
let date = new Date(d);
|
||||
// if the date is an invalid string then creating a date from the string will fail and N/A is returned
|
||||
if (isNaN(date.valueOf())) {
|
||||
return 'N/A';
|
||||
}
|
||||
return `${numberPadding(date.getDate())}.${numberPadding(date.getMonth() + 1)}.${date.getFullYear()}`;
|
||||
}
|
||||
// parameter is not of type string or Date and an exception is thrown
|
||||
else
|
||||
{
|
||||
throw new TypeError("The parameter provided for this function is not a string or a Date object, please refere to the function formatDate in the DateHelpers.js file");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user