CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/components/news/useDateStr.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2023 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import dayjs from "dayjs";
7
import { useMemo } from "react";
8
9
import { NewsItem } from "@cocalc/util/types/news";
10
11
export function useDateStr(
12
news?: Omit<NewsItem, "text">,
13
minutes = false,
14
format="YYYY-MM-DD"
15
): string {
16
const f = minutes ? "YYYY-MM-DD HH:mm" : format;
17
return useMemo(() => {
18
if (news == null) return "";
19
if (typeof news.date === "number") {
20
return dayjs(news.date * 1000).format(f);
21
} else {
22
return `${news.date}`;
23
}
24
}, [news?.date]);
25
}
26
27