generated from alphane/template
19 lines
650 B
TypeScript
19 lines
650 B
TypeScript
import { isNil, isEmpty } from "lodash"
|
|
import { DateTime } from "luxon"
|
|
|
|
export function formatDate(input: string | Date | undefined): string {
|
|
if (isNil(input) || isEmpty(input)) {
|
|
return ""
|
|
} else if (typeof input == "string") {
|
|
const parsed = DateTime.fromISO(input, { zone: "utc" })
|
|
const isMidnight = parsed.hour === 0 && parsed.minute === 0 && parsed.second === 0
|
|
return isMidnight ? parsed.toFormat("yyyy-MM-dd") : parsed.toLocal().toFormat("yyyy-MM-dd")
|
|
} else if (input instanceof Date) {
|
|
return DateTime.fromJSDate(input).toLocal().toFormat("yyyy-MM-dd")
|
|
} else {
|
|
return ""
|
|
}
|
|
}
|
|
|
|
export default formatDate
|