0

Within a rails app I have a .jst.eco template which includes the line...

<span> <%= @event.get 'create_at' %> </span> 

...which renders the date like so 2015-01-17 11:04:03.

However I only want to display 2015-01-17. Is there a neat formatting trick I can use within the <%= ... %>?

2 Answers 2

1

You can write as:

span> <%= @event.get('create_at').to_s(:db) %> </span>
# or
span> <%= @event.get('create_at').to_formatted_s(:db) %> </span>

:db means '%Y-%m-%d. Check out DATE_FORMATS.

Sign up to request clarification or add additional context in comments.

Comments

0

It depends on the object you have. If it is a time object, you can do:

@event.get('create_at').strftime('%F')

if it is just a string, then first you should parse it:

Time.parse(@event.get('create_at')).strftime('%F')

According to .strftime docs %F is The ISO 8601 date format (%Y-%m-%d)

1 Comment

Best answer because of the string comment. Thanks alot.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.