Files
spacedrive/interface/app/$libraryId/Explorer/View/MediaView/util.ts
Artsiom Voitas 95b9ba7e9c Added date formatting according to chosen language in the settings (#2414)
* added date formatting according to chosen language

* deleted spaces

* deleted spaces

* fixed typos

* set date formats after

* error handling

---------

Co-authored-by: Utku Bakir <74243531+utkubakir@users.noreply.github.com>
2024-04-30 16:52:20 +00:00

42 lines
1.0 KiB
TypeScript

import dayjs from 'dayjs';
import { ExplorerItem, getExplorerItemData, OrderingKey } from '@sd/client';
export const formatDate = (date: Date | { from: Date; to: Date }, dateFormat: string) => {
if (date instanceof Date) return dayjs(date).format(dateFormat);
const sameMonth = date.from.getMonth() === date.to.getMonth();
const sameYear = date.from.getFullYear() === date.to.getFullYear();
const fromDateFormat = ['D', !sameMonth && 'MMM', !sameYear && 'YYYY']
.filter(Boolean)
.join(' ');
return `${dayjs(date.from).format(fromDateFormat)} - ${dayjs(date.to).format(dateFormat)}`;
};
export function getDate(item: ExplorerItem, orderBy: OrderingKey) {
const filePath = getExplorerItemData(item);
switch (orderBy) {
case 'dateCreated': {
return filePath.dateCreated;
}
case 'dateIndexed': {
return filePath.dateIndexed;
}
case 'dateModified': {
return filePath.dateModified;
}
case 'object.dateAccessed': {
return filePath.dateAccessed;
}
case 'object.mediaData.epochTime': {
return filePath.dateTaken;
}
}
}