Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizing stringify to Format JavaScript Date Objects as TOML v1.0.0 Local Dates #28

Closed
maliknajjar opened this issue Sep 27, 2024 · 3 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@maliknajjar
Copy link

I have a JavaScript object that contains a property which is a Date object. Currently, when I use the stringify function, it converts the Date object to this format:

image

Is there a way to customize the stringify function so that it converts Date properties into a local date without the time component?

https://toml.io/en/v1.0.0#local-date
image

@cyyynthia
Copy link
Member

It is the documented behavior for date objects indeed, to use other formats you'll need to use TomlDate. Note that you can treat it as a normal date object, it is perfectly compatible and will behave as expected (including with the type system)

@maliknajjar
Copy link
Author

I initially tried using TomlDate in my library, and it didn’t work as expected. However, after your comment, I tested it in a plain JavaScript project, and it functioned correctly, which indicates that the issue was within my library.

I understand that TomlDate is fully compatible when TOML is handled between machines. However, when users frequently interact with the TOML file and view the TOML string, it makes sense to simplify the date representation. Reducing unnecessary details in the date property can enhance clarity and make it easier for users to understand the TOML.

Thank you so much for your help. it's fixed now! I truly appreciate this library and you work for the community.

@cyyynthia cyyynthia added enhancement New feature or request question Further information is requested labels Sep 29, 2024
@cyyynthia
Copy link
Member

cyyynthia commented Sep 29, 2024

I understand that TomlDate is fully compatible when TOML is handled between machines. However, when users frequently interact with the TOML file and view the TOML string, it makes sense to simplify the date representation. Reducing unnecessary details in the date property can enhance clarity and make it easier for users to understand the TOML.

I'm unsure I understand that part; TomlDate extends the Date object so that it can be used transparently within your code, but preserves all the applicable semantics for what type of date it should serialize as. If a document is parsed and the document used a Local Date, the parser will emit a TomlDate that'll serialize back as a Local Date (and not a full datetime w/ tz). (more specifically, the toISOString method returns only the date).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants