Skip to content

Create date-links based on natural language in Obsidian

Notifications You must be signed in to change notification settings

vslinko/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 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.

Obsidian URI New in v0.4.0

It's now possible to use the Obsidian URI to open daily notes using natural language by using the nldates action obsidian://nldates?day=<date here>. Don't forget to encode space characters appropriately.

obsidian://nldates Parameter Description
day natural language date string
newPane open note in new pane, default is yes

Commands and hotkeys

Starting on v0.3.2, in addition to the hotkey to parse the selected date, the following commands are also available (note that hotkeys are unset by default starting on v0.4.1):

Natural Language Dates: Date Picker

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

Natural Language Dates: Insert current date

Inserts the current date, using the format specified in the settings menu. (default output YYYY-MM-DD)

Natural Language Dates: Insert current time

Inserts the current time, using the format specified in the settings menu. (default output HH:mm)

Natural Language Dates: Insert current date and time

Inserts the current date, using the format specified in the settings menu. (default output YYYY-MM-DD HH:mm)

Natural Language Dates: Parse natural language date

Parses the selected text as a natural language date. Replaces selected text with an obsidian link to the parsed date in the format specified in the settings menu (default [[YYYY-MM-DD]]).

Natural Language Dates: Parse natural language time

Parses the selected text as a natural language time. Replaces selected text with the parsed time stamp in the format specified in the settings menu (default HH:mm).

You can try with any of the standard times, i.e. now, in 15min, in 1h, 5min ago, etc.

Natural Language Dates: Parse natural language date (as link)

Parses the selected text as a natural language date. Replaces selected text with a standard markdown link to the parsed date in the format specified in the settings menu (default [selected text](YYYY-MM-DD)).

Natural Language Dates: Parse natural language date (as plain text)

Parses the selected text as a natural language date. Replaces selected text with a plain text parsed date in the format specified in the settings menu (default YYYY-MM-DD).

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

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.9%
  • JavaScript 2.7%
  • CSS 0.4%