aps-data-api
Version:
package for data extraction from APS company for omnimetic project
43 lines (36 loc) • 1.12 kB
text/typescript
import moment from 'moment';
import { ShortMonth } from '../typings';
export const formatDate = (inputDate: Date): string =>
moment(inputDate).format('YYYY-MM-DD');
export const formatHour = (hour: string): string => {
const hourSplit = hour.split(' ');
if (hourSplit[0] === '12:00' && hourSplit[1] === 'AM') {
return '00:00';
}
if (hourSplit[0] === '12:00' && hourSplit[1] === 'PM') {
return hourSplit[0];
}
if (hourSplit[1] === 'PM') {
return `${12 + Number(hourSplit[0].slice(0, 2))}:00`;
}
return hourSplit[0];
};
export const formatBillDate = (inputDate: string): string => {
return `${inputDate.split('_')[1]}_${
ShortMonth[inputDate.split('_')[0] as keyof typeof ShortMonth]
}`;
};
export const diffInDays = (startDate: Date, endDate: Date) => {
const MS_PER_DAY = 1000 * 60 * 60 * 24;
const utcStart = Date.UTC(
startDate.getFullYear(),
startDate.getMonth(),
startDate.getDate(),
);
const utcEnd = Date.UTC(
endDate.getFullYear(),
endDate.getMonth(),
endDate.getDate(),
);
return Math.floor((utcEnd - utcStart) / MS_PER_DAY);
};