projects/maplander/components/src/lib/utils/classes/mDate.ts
Properties |
|
Methods |
|
constructor(date: number)
|
||||||||
Parameters :
20190812 |
Private _timestamp |
Type : string
|
Private getDay |
getDay()
|
Returns :
string
|
Public getFormat | ||||||||||||||
getFormat(format: "dd/MM/yyyy" | "dd MMMMM yyyy" | "dd MMMMM" | "EEEEE HH:mm" | "dd MMMMM HH:mm" | "dd MMMMM yyyy HH:mm", undefined: object)
|
||||||||||||||
Parameters :
Returns :
string
string Return string with date format |
Private Static getHours | ||||||
getHours(time: string)
|
||||||
Parameters :
Returns :
string
|
Private Static getMinutes | ||||||
getMinutes(time: string)
|
||||||
Parameters :
Returns :
string
|
Private Static getMonthName |
getMonthName(month: string, toLowerCase?: boolean)
|
Returns :
string
|
Private getMoth |
getMoth()
|
Returns :
string
|
Private Static getSeconds | ||||||
getSeconds(time: string)
|
||||||
Parameters :
Returns :
string
|
Static getTimestamp | ||||||||||||
getTimestamp(date: Date, time?: boolean)
|
||||||||||||
Parameters :
Example :
Returns :
number
number |
Private Static getWeekDayName |
getWeekDayName(day: number, toLowerCase?: boolean)
|
Returns :
string
|
Private getYear |
getYear()
|
Returns :
string
|
Public toDate | ||||||||
toDate(time?: number)
|
||||||||
Parameters :
Example :
Returns :
Date
Date Converting timestamp with format YYYYMMDD, if you not assign the time param, the hours, minutes and seconds is a zero |
export class MDate {
private _timestamp: string;
/**
* @param date Timestamp with format YYYYMMDD
* @example 20190812
* */
constructor(date: number) {
this._timestamp = date.toString();
}
/**
* @param date Date object
* @param time Returns timestamp as time, default is date
* @example Date: Format YYYYMMDD = 20190812, Time: Format HHMMSS = 171401
* @return number
* */
public static getTimestamp(date: Date, time?: boolean): number {
let timestamp = '';
if (time) {
timestamp += `${date.getHours()}`;
timestamp += date.getMinutes() < 10 ? `0${date.getMinutes()}` : `${date.getMinutes()}`;
timestamp += date.getSeconds() < 10 ? `0${date.getSeconds()}` : `${date.getSeconds()}`;
return Number(timestamp);
} else {
timestamp += `${date.getFullYear().toString()}`;
timestamp += (date.getMonth() + 1) < 10 ? `0${(date.getMonth() + 1)}` : `${date.getMonth() + 1}`;
timestamp += date.getDate() < 10 ? `0${date.getDate()}` : `${date.getDate().toString()}`;
return Number(timestamp);
}
}
private static getHours(time: string): string {
return time.slice(0, 2);
}
private static getMinutes(time: string): string {
return time.slice(2, 4);
}
private static getSeconds(time: string): string {
return time.slice(4, time.length);
}
private static getWeekDayName(day: number, toLowerCase?: boolean): string {
switch (day) {
case 0:
return toLowerCase ? 'lunes' : 'Lunes';
case 1:
return toLowerCase ? 'martes' : 'Martes';
case 2:
return toLowerCase ? 'miercoles' : 'Miercoles';
case 3:
return toLowerCase ? 'jueves' : 'Jueves';
case 4:
return toLowerCase ? 'viernes' : 'Viernes';
case 5:
return toLowerCase ? 'sabado' : 'Sabado';
case 6:
return toLowerCase ? 'domingo' : 'Domingo';
}
}
private static getMonthName(month: string, toLowerCase?: boolean): string {
switch (month) {
case '01':
return toLowerCase ? 'ene.' : 'Ene.';
case '02':
return toLowerCase ? 'feb.' : 'Feb.';
case '03':
return toLowerCase ? 'mar.' : 'Mar.';
case '04':
return toLowerCase ? 'abr.' : 'Abr.';
case '05':
return toLowerCase ? 'may.' : 'May.';
case '06':
return toLowerCase ? 'jun.' : 'Jun.';
case '07':
return toLowerCase ? 'jul.' : 'Jul.';
case '08':
return toLowerCase ? 'ago.' : 'Ago.';
case '09':
return toLowerCase ? 'sep.' : 'Sep.';
case '10':
return toLowerCase ? 'oct.' : 'Oct.';
case '11':
return toLowerCase ? 'nov.' : 'Nov.';
case '12':
return toLowerCase ? 'dic.' : 'Dic.';
}
}
/**
* @param time Timestamp with format HHMMSS
* @example 171401
* @return Date
* Converting timestamp with format YYYYMMDD,
* if you not assign the time param, the hours, minutes and seconds is a zero
* */
public toDate(time?: number): Date {
if (time) {
const cDate = `${this.getYear()}-${this.getMoth()}-${this.getDay()}`;
const cTime = `${MDate.getHours(time.toString())}:${MDate.getMinutes(time.toString())}:${MDate.getSeconds(time.toString())}`;
const fullCDate = `${cDate}T${cTime}Z`;
return new Date(fullCDate);
} else {
return new Date(Number(this.getYear()), Number(this.getMoth()) - 1, Number(this.getDay()));
}
}
/**
* @param format Format date | 'dd/MM/yyyy' | 'dd MMMMM yyyy' | 'EEEEE HH:mm' | 'dd MMMMM HH:mm' |'dd MMMMM yyyy HH:mm'
* @param time Timestamp as time with format HHMMSS
* @param toLowerCase Words on lower case
* @param format12Hours 12 hours format on time
* @return string
* Return string with date format
* */
public getFormat(format: 'dd/MM/yyyy' | 'dd MMMMM yyyy' | 'dd MMMMM' | 'EEEEE HH:mm' | 'dd MMMMM HH:mm' | 'dd MMMMM yyyy HH:mm', {
time = null,
toLowerCase = false,
format12Hours = false
} = {}): string {
let date = '', d: Date = null;
switch (format) {
case 'dd/MM/yyyy':
date = `${this.getDay()}/${this.getMoth()}/${this.getYear()}`;
break;
case 'dd MMMMM yyyy':
date = `${this.getDay()} ${MDate.getMonthName(this.getMoth(), toLowerCase)} ${this.getYear()}`;
break;
case 'dd MMMMM':
date = `${this.getDay()} ${MDate.getMonthName(this.getMoth(), toLowerCase)}`;
break;
case 'EEEEE HH:mm':
date += `${MDate.getWeekDayName(this.toDate(time).getDay() - 1, toLowerCase)}`;
d = this.toDate(time);
if (format12Hours) {
date += d.getHours() === 13 ? ` a la ` : ` a las `;
date += d.getHours() < 12 ? d.getHours() : d.getHours() - 12;
date += ':' + d.getMinutes();
date += d.getHours() < 12 ? ` AM` : ` PM`;
} else {
date += ' a las ';
date += `${d.getHours()}:${d.getMinutes()}`;
}
break;
case 'dd MMMMM HH:mm':
date += `${this.getDay()}`;
date += ` de ${MDate.getMonthName(this.getMoth(), toLowerCase)}`;
d = this.toDate(time);
if (format12Hours) {
date += d.getHours() === 13 ? ` a la ` : ` a las `;
date += d.getHours() < 12 ? d.getHours() : d.getHours() - 12;
date += ':' + d.getMinutes();
date += d.getHours() < 12 ? ` AM` : ` PM`;
} else {
date += ' a las ';
date += `${d.getHours()}:${d.getMinutes()}`;
}
break;
case 'dd MMMMM yyyy HH:mm':
date += `${this.getDay()}`;
date += ` de ${MDate.getMonthName(this.getMoth(), toLowerCase)} ${this.getYear()}`;
d = this.toDate(time);
if (format12Hours) {
date += d.getHours() === 13 ? ` a la ` : ` a las `;
date += d.getHours() < 12 ? d.getHours() : d.getHours() - 12;
date += ':' + d.getMinutes();
date += d.getHours() < 12 ? ` AM` : ` PM`;
} else {
date += ' a las ';
date += `${d.getHours()}:${d.getMinutes()}`;
}
break;
}
return date;
}
private getYear(): string {
return this._timestamp.slice(0, 4);
}
private getMoth(): string {
return this._timestamp.slice(4, 6);
}
private getDay(): string {
return this._timestamp.slice(6, this._timestamp.length);
}
}