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

Add function and filter to convert string to date #608

Merged
merged 5 commits into from
Mar 4, 2021

Conversation

liamrharwood
Copy link
Contributor

We currently have the strtotime function and filter that can be used to convert a string object into a DateTime object. However, we don't have a good way to do this for Date objects that don't include a time.

For instance, there's no good way to convert the string 3/4/21 to a different format, since it doesn't include the time.

This PR adds the new strtodate function/filter that can convert dates without time.

For instance, you can do something like this:

{{ "3/4/21"|strtodate('d/M/yy')|datetimeformat('%B %e, %Y') }}

To output the following:
March 4, 2021

@boulter
Copy link
Contributor

boulter commented Mar 4, 2021

Should we call it strptime? That seems like the closest equivalent in python and what someone would use in Jinja.

https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

@liamrharwood
Copy link
Contributor Author

I'm not against it, but we already have strtotime so I thought strtodate would be consistent.

@mattcoley
Copy link
Collaborator

Why did we name it strtotime in the first place?

@mattcoley
Copy link
Collaborator

#258 👎

return null;
}

if (!(var instanceof String)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We could write the object to String here

@liamrharwood liamrharwood merged commit 7cc0e6b into master Mar 4, 2021
@liamrharwood liamrharwood deleted the str-to-date-filter branch March 4, 2021 18:53
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.

4 participants