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 |
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):
Inserts the current date, using the format specified in the settings menu. (default output YYYY-MM-DD
)
Inserts the current time, using the format specified in the settings menu. (default output HH:mm
)
Inserts the current date, using the format specified in the settings menu. (default output YYYY-MM-DD HH:mm
)
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]]
).
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.
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)
).
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.
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:
In Obsidian go to Settings > Third-party plugins > Community Plugins > Browse
and search for Natural Language Dates
.
Unzip the latest release into your <vault>/.obsidian/plugins/
folder.
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 ofnldates
and without the square brackets. -
The
date
object is what is returned by theparseDate
method of the custom parser (using the chrono package). -
The
Moment
object is created with thedate
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
, thedate
andformattedString
won't be updated. If you don't want to alter theparsedResult.moment
, you should clone it. Read more about that here.