All files / utils timeUtilities.ts

75% Statements 96/128
100% Branches 3/3
11.53% Functions 3/26
75% Lines 96/128

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 1301x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x        
// Import instance
import { UserNotification } from "@/common/models";
import dayjs from "dayjs"
 
/**
 * @param  {any} date
 * @returns string
 * By: Eko Sutrisno
 */
export const formatDateFromNow = (date: any): string => {
    return dayjs(date).fromNow();
}
 
/**
 * @param  {any} date
 * @returns string
 * By: Eko Sutrisno
 */
export const formatDateWithMonth = (date: any): string => {
    return dayjs(date)
        .format('LL')
}
 
/**
* @param  {any} date
* @returns string
* By: Eko Sutrisno
*/
export const formatDateWithDayMonth = (date: any): string => {
    return dayjs(date)
        .format('dddd, MMMM D, YYYY');
}
 
/**
* @param  {any} date
* @returns string
* By: Eko Sutrisno
*/
export const formatDateMonth = (date: any): string => {
    return dayjs(date)
        .format('YYYY-MM-DD');
}
 
/**
* Get Year-Month format
* @returns string
* By: Eko Sutrisno
*/
export const currentMonth = (): string => {
    const currentDate = dayjs();
    return `${currentDate.year()}-${currentDate.month() + 1}`
}
 
/**
* Get Extract Year-Month format
* @returns string
* By: Eko Sutrisno
*/
export const extractCurrentMonthYear = (date: any): string => {
    const currentDate = dayjs(date);
    return `${currentDate.year()}-${currentDate.month() + 1}`
}
 
/**
* Get Month format
* @returns string
* By: Eko Sutrisno
*/
export const currentMonthOnly = (): string => {
    return `${dayjs().month() + 1}`
}
 
/**
* To Check is Weekend
* @returns boolean
* By: Eko Sutrisno
*/
export const isWeekend = (date: any): boolean => {
    const currentDate = dayjs(date);
    return currentDate.day() === 0 || currentDate.day() === 6
}
 
/**
* To Check is Today Timesheet
* @returns boolean
* By: Eko Sutrisno
*/
export const isToday = (date: any): boolean => {
    return dayjs().format('l') === dayjs(date).format('l');
}
 
/**
* To return current Month And Year only
* @returns string
* By: Eko Sutrisno
*/
export const currentMonthAndYear = (date: any): string => {
    return dayjs(date).format('MMMM YYYY')
}
/**
 * @param  {} date
 * it get previous day, aka. Yestereday
 * By: Eko Sutrisno
 */
export const getPreviousDay = (date = new Date()) => {
    const previous = new Date(date.getTime());
    previous.setDate(date.getDate() - 1);

    return previous;
}
 
/**
 * @param  {string} date
 * This will remove timezone embeded from server and make relative with the user current time.
 * By: Eko Sutrisno
 */
export const removeTimezone = (date: string)=>{
    return date;
}
 
/**
 * @param  {UserNotification[]} array
 * it will make the notification ascending order and get latest notification first
 * By: Eko Sutrisno
 */
export const orderDescending = (array:UserNotification[])=> {
    return array.sort((a: any,b: any)=> b.read && (new Date(b.createdAt).valueOf()) - (new Date(a.createdAt).valueOf()));
}