SolarEnergy/central_frontend/src/utils/DateUtils.ts

30 lines
686 B
TypeScript

import dayjs, { Dayjs } from "dayjs";
/**
* Get current UNIX time, in seconds
*/
export function time(): number {
return Math.floor(new Date().getTime() / 1000);
}
/**
* Get dayjs representation of given time of day
*/
export function timeOfDay(time: number): Dayjs {
const hours = Math.floor(time / 3600);
const minutes = Math.floor(time / 60) - hours * 60;
return dayjs(
`2022-04-17T${hours.toString().padStart(2, "0")}:${minutes
.toString()
.padStart(2, "0")}`
);
}
/**
* Get time of day (in secs) from a given dayjs representation
*/
export function dayjsToTimeOfDay(d: Dayjs): number {
return d.hour() * 3600 + d.minute() * 60 + d.second();
}