@benjypng/logseq-dateutils
v3.0.1
Published
  ), with or without[[page brackets]]. - Ready-made org-style lines: Generate complete
SCHEDULED: <...>andDEADLINE: <...>strings, with the time included automatically when it isn't midnight. - Compact timestamps:
yyMMddandyyMMdd'T'HHmmhelpers for IDs and filenames. - Dual ESM + CJS with full TypeScript declarations for both formats.
- Tiny: under 9 kB minified + gzipped including its only dependency,
date-fns.
⚙️ Installation
npm i @benjypng/logseq-dateutilsImport (you may also import only selective functions for your needs):
import {
getDateForPage,
getDateForPageWithoutBrackets,
getDeadlineDateDay,
getScheduledDateDay,
getYYMMDDTHHMMFormat,
getYYMMDD,
} from '@benjypng/logseq-dateutils';🛠 Usage
getDateForPage or getDateForPageWithoutBrackets
Returns the specified date based on the user's preferred date format. Accepts 2 arguments: a Date and the user's preferred date format (e.g. from logseq.App.getUserConfigs()). getDateForPage returns the date with brackets ([[date]]) while getDateForPageWithoutBrackets returns the date without the brackets.
import { getDateForPage, getDateForPageWithoutBrackets } from '@benjypng/logseq-dateutils';
const preferredDateFormat = 'MMM do, yyyy';
const today = new Date();
getDateForPage(today, preferredDateFormat);
// e.g. '[[Oct 3rd, 2023]]'
getDateForPageWithoutBrackets(today, preferredDateFormat);
// e.g. 'Oct 3rd, 2023'getScheduledDateDay
Returns a complete SCHEDULED: line for the specified date, ready to insert into a block. The time is included only if it is not midnight.
const today = new Date();
await logseq.Editor.updateBlock(
uuid,
`A quick brown fox
${getScheduledDateDay(today)}`
);
// Appends e.g. 'SCHEDULED: <2023-10-03 Tue>'
// or 'SCHEDULED: <2023-10-03 Tue 12:35>' if a time is setgetDeadlineDateDay
Same as getScheduledDateDay, but returns a DEADLINE: line instead.
const today = new Date();
await logseq.Editor.updateBlock(
uuid,
`A quick brown fox
${getDeadlineDateDay(today)}`
);
// Appends e.g. 'DEADLINE: <2023-10-03 Tue>'
// or 'DEADLINE: <2023-10-03 Tue 12:35>' if a time is setgetYYMMDDTHHMMFormat
Returns the specified date and time in yyMMdd'T'HHmm format.
getYYMMDDTHHMMFormat(new Date('2023-10-03T12:35:00'));
// '231003T1235'getYYMMDD
Returns the specified date in yyMMdd format.
getYYMMDD(new Date('2023-10-03T12:35:00'));
// '231003'☕️ Support
If you find this library useful, please consider supporting the development.
🤝 Contributing
Issues are welcome. If you find a bug, please open an issue — or join Logseq's Discord and look for me there. Pull requests are not accepted at the moment as I am not able to commit to reviewing them in a timely fashion.
