Install Dayjs
เริ่มจากการติดตั้ง Dayjs กันก่อน npm install dayjs
หรือ yarn add dayjs
การใช้งาน
ในตัวอย่างนี้ผมจะใช้วิธีการ import แบบ ES6 นะครับ
import dayjs from "dayjs";
import "dayjs/locale/th";
import buddhistEra from "dayjs/plugin/buddhistEra";
dayjs.extend(buddhistEra); // ใช้งาน buddhistEra plugin เพื่อแปลงเป็น พ.ศ.
/* 07 กุมภาพันธ์ 2566 */
export const DateLongTH = (date) => {
dayjs.locale("th");
return dayjs(date).format("DD MMMM BBBB");
};
/* 07 ก.พ. 2566 */
export const DateShortTH = (date) => {
dayjs.locale("th");
return dayjs(date).format("DD MMM BB");
};
/* 07 February 2023 */
export const DateLongEN = (date) => {
dayjs.locale("en");
return dayjs(date).format("DD MMMM YYYY");
};
/* 07 Feb 23 */
export const DateShortEN = (date) => {
dayjs.locale("en");
return dayjs(date).format("DD MMM YY");
};
หรือจะเอา TypeScript มาใช้ก็ได้ครับ
import dayjs from "dayjs";
import "dayjs/locale/th";
import buddhistEra from "dayjs/plugin/buddhistEra";
dayjs.extend(buddhistEra);
/* 07 กุมภาพันธ์ 2566 */
export const DateLongTH = (date: Date) => {
dayjs.locale("th");
return dayjs(date).format("DD MMMM BBBB");
};
/* 07 ก.พ. 2566 */
export const DateShortTH = (date: Date) => {
dayjs.locale("th");
return dayjs(date).format("DD MMM BB");
};
/* 07 February 2023 */
export const DateLongEN = (date: Date) => {
dayjs.locale("en");
return dayjs(date).format("DD MMMM YYYY");
};
/* 07 Feb 23 */
export const DateShortEN = (date: Date) => {
dayjs.locale("en");
return dayjs(date).format("DD MMM YY");
};