Skip to content

Conversation

@spozer
Copy link
Contributor

@spozer spozer commented Jul 24, 2023

This adds the option to set a custom date format, like requested in #78 by @iznaut.

I added a DateFormatter class, which gets used in the APIModels to format date strings. I also looked up what type of date formats are used by the various APIs, and saved them as variables called apiDateFormat. This makes the date formatting more robust, because you don't have to guess the actual date formats.

The DateFormatter holds the currently set custom date format. This gets updated in the main - by calling the setFormat() function of DateFormatter - when the user changes the custom date format in the settings tab.

For the settings tab, I added a text field where the user can input a custom date format string. The settings also shows a preview of what the current format will look like.
image

For the actual date formatting the DateFormatter uses moment.js. Which is also used and exported by obsidian itself. But I had some trouble importing it. With esModuleInterop set in the compiler config, the type script compiler doesn't like that obsidian imports moment with a namespace-style import (which is also not compliant with ES6 module spec) like this:

// obsidian.ts
import * as Moment from 'moment';
export const moment: typeof Moment;

So when running npm run build the following always results in compiler errors complaining about the namespace-style import of moment in obsidian.ts:

// DateFormatter.ts
import { 'moment' } from 'obsidian';

I eventually got it to work with:

// DateFormatter.ts
const obsidian = require('obsidian');
const moment = obsidian.moment;

I don't know if this is a good way to import it though.

@mProjectsCode
Copy link
Owner

Thanks for the PR. I am currently quite busy, but I will try to review the PR before the weekend.

@spozer
Copy link
Contributor Author

spozer commented Jul 26, 2023

I just noticed that there is an error when using other locale formats other then 'en' or 'gb'. I will try to fix this.

… display date preview in settings panel, also prefixed element id with media-db
@spozer spozer requested a review from mProjectsCode August 28, 2023 15:43
Copy link
Owner

@mProjectsCode mProjectsCode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mProjectsCode mProjectsCode merged commit e416434 into mProjectsCode:master Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants