Skip to content

Create date-links based on natural language in Obsidian

Notifications You must be signed in to change notification settings

nidongpinyinme/nldates-obsidian

 
 

Repository files navigation

Natural language dates in Obsidian

Create date links using natural language processing using chrono and some custom parsing. To create a date link, select the text you want to change (e.g. today), and use the NLP date command. You can use the shortcut (default CTRL + Y) or the command palette (Ctrl + P). For single-word dates (e.g. today, tomorrow, friday, etc.), it's possible to use the command without selecting the word first. It's also possible to use dates like Nov9, 25Dec to use this trick.

You can try with any of the standard dates, i.e. today, tomorrow, in 3 weeks, in 5 months, etc. The only behaviours I changed were the following:

Write Date
next week next Monday
next [month] 1st of next month
mid [month] 15th of the month
end of [month] last day of the month

If a date is not recognized, the link won't be created.

Commands and hotkeys

Starting on v0.3.1, in addition to the hotkey to parse the selected date, the following commands are also available:

Three commands to insert the current date, current time and a combination of both.


You can of course add hotkeys to each of these commands.

Natural language date picker


Three commands to insert the current date, current time and a combination of both.

Demo

demo

Note: The parser will replace all the selected text, meaning that in a sentence you should only select the dates to be parsed and not the full sentence.
In the example sentence Do this thing by tomorrow, only the word tomorrow should be selected. Alternatively, keep in mind that you can place your cursor on or next to the word tomorrow, and it will be replaced:

Supported selections

How to install

In Obsidian go to Settings > Third-party plugins > Community Plugins > Browse and search for Natural Language Dates.

Manual installation

Unzip the latest release into your <vault>/.obsidian/plugins/ folder.

For Developers

You can use the method parsedDate to parse dates. It takes a string as an argument, and returns a NLDResult:

interface NLDResult {
  formattedString: string;
  date: Date;
  moment: any; // This is actually a Moment object
}
  • The formattedString will return the date formatted according to the settings of nldates and without the square brackets.

  • The date object is what is returned by the parseDate method of the custom parser (using the chrono package).

  • The Moment object is created with the date object, cloning it. If you need, you can further manipulate or format the moment object, for example:

    let nldatesPlugin = obsidianApp.plugins.getPlugin('nldates-obsidian');
    let parsedResult = nldatesPlugin.parseDate("next year");
    console.log(parsedResult.moment.format("YYYY")); //This should return 20201
    console.log(parsedResult.moment.fromNow()) // "In two months"
    parsedResult = nldatesPlugin.parseDate("today at 21:00");
    console.log(parsedResult.moment.add(1, "hour")); // This would change the Moment to 22:00

    Note that if you manipulate the parsedResult.moment, the date and formattedString won't be updated. If you don't want to alter the parsedResult.moment, you should clone it. Read more about that here.

About

Create date-links based on natural language in Obsidian

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 96.2%
  • JavaScript 3.3%
  • CSS 0.5%