# Converting datetimeformat for StringToTime

**URL:** <https://community.hubspot.com/t/converting-datetimeformat-for-stringtotime/17886>\
**Category:** CMS Development\
**Tags:** content\
**Created:** [January 3, 2020, 5:34pm UTC](https://community.hubspot.com/t/converting-datetimeformat-for-stringtotime/17886 "2020-01-03T17:34:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![david-at-bold](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/david-at-bold/32/87988_2.png) [@david-at-bold](https://community.hubspot.com/u/david-at-bold)\
**Post date:** [January 3, 2020, 5:34pm UTC](https://community.hubspot.com/t/converting-datetimeformat-for-stringtotime/17886/1 "2020-01-03T17:34:44Z")

</div>

Hey, I’m trying to print out the time of a published post depending on how many days it has been since it was posted. After researching quite a bit, I’m stuck at trying to pass on the variable publishDate so I can use the between\_times filter to figure out the time between 2 dates.

Here is my code:

```auto
Published</br>
{% set publishDate = post.publish_date|datetimeformat('%Y-%m-%d %H:%M:%S') %}
{% set currentDate = local_dt|datetimeformat('%Y-%m-%d %H:%M:%S') %}

{% set begin = publishDate|strtotime("yyyy-MM-dd'T'HH:mm:ssZ") %}
{% set end =currentDate|strtotime("yyyy-MM-dd'T'HH:mm:ssZ") %}
{% set hours_since_published_date = begin|between_times(end, 'hours') %}

 {% if hours_since_publish_date < 24 %}
 <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}">today</time>
 {% elif hours_since_publish_date >= 24 and hours_since_publish_date < 48 %}
 <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}">yesterday</time>
 {% elif hours_since_publish_date >= 48 %}
 <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}">{{ hours_since_publish_date}} days ago</time>
 {% endif %} by
 <a href="#" class="authorText">{{ post.blog_author.display_name }}</a>

```

I end up getting errors with the following two lines: strongToTime could not match dateTime input with dateTime format.

```auto
{% set begin = publishDate|strtotime("yyyy-MM-dd'T'HH:mm:ssZ") %}
{% set end = currentDate|strtotime("yyyy-MM-dd'T'HH:mm:ssZ") %}

```

I’ve been trying to figure out how I can convert a dateTIme into a dateTIme object (or if that’s even possible)

---

<div class="post-metadata">

**Author:** ![Kevin-C](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kevin-c/32/34570_2.png) [@Kevin-C](https://community.hubspot.com/u/Kevin-C)\
**Post date:** [January 6, 2020, 12:29am UTC](https://community.hubspot.com/t/converting-datetimeformat-for-stringtotime/17886/2 "2020-01-06T00:29:20Z")

</div>

Hey @david-at-bold

I’d recommend not transforming any data until it’s time to print it or assigning unneeded variables. Transforming before a comparrsion will make it a lot harder to debug than it has to be.

Your code also has 2 major issues:

1) You’ve missepelled your hours varieble you set it as “ **hours\_since\_published\_date** ” then later try to reference/use it as “ **hours\_since\_publish\_date** ”.

2) Also on line 5 of the code block you posted you’re missing a space when declaring the “end” variable.

I used this code to test, and it seems to work as expected:

```auto
{% set pop_posts = blog_recent_posts('default', 100) %}

{% for post in pop_posts %}

{% set publishDate = post.publish_date %}
{% set currentDate = local_dt %}

{% set begin = publishDate %}
{% set hours_since_publish_date = begin|between_times(currentDate, 'hours') %}

 {% if hours_since_publish_date < 10000 %}
 <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}">today</time>
 {% elif hours_since_publish_date >= 24 and hours_since_publish_date < 48 %}
 <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}">yesterday</time>
 {% elif hours_since_publish_date >= 48 %}
 <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}">{{ hours_since_publish_date}} days ago</time>
 {% endif %}

{% endfor %}

```

_You’ll see the for loop I added for my testing purposes_

Hope this helps get you moving!

---

<div class="post-metadata">

**Author:** ![david-at-bold](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/david-at-bold/32/87988_2.png) [@david-at-bold](https://community.hubspot.com/u/david-at-bold)\
**Post date:** [January 6, 2020, 5:45pm UTC](https://community.hubspot.com/t/converting-datetimeformat-for-stringtotime/17886/3 "2020-01-06T17:45:34Z")

</div>

I see what you did. It works!

I thought you have to use the datetimeformat filter to actually ge the time. Here’s my code with examples for today, yesterday, 3-5 days ago, last week, and then published date. I probably coudl’ve simplified by getting the between\_times filter to give out days instead of hours.

```auto
{% set posts = blog_recent_posts('default', 200) %}{% for post in posts %} Published
 {% set publishDate = post.publish_date %}
 {% set currentDate = local_dt %}

 {% set begin = publishDate %}

 {% set hours_since_publish_date = begin|between_times(currentDate, 'hours') %}
 {% set days_since_publish_date = (hours_since_publish_date / 24)|round %}

 {% if hours_since_publish_date < 24 %}
 <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}">today</time>
 {% elif hours_since_publish_date >= 24 and hours_since_publish_date < 48 %}
 <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}">yesterday</time>
 {% elif hours_since_publish_date >= 48 and hours_since_publish_date < 120 %}
 <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}">{{ days_since_publish_date }} days ago</time>
 {% elif hours_since_publish_date >= 120 and hours_since_publish_date < 240 %}
 <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}"> last week</time>
 {% elif hours_since_publish_date >= 240 %}
 <time class="timeStamp" datetime="{{ post.publish_date|datetimeformat('%B %e, %Y') }}">{{ post.publish_date|datetimeformat('%B %e, %Y') }}</time>
 {% endif %} by
 <a href="#" class="authorText">{{ post.blog_author.display_name }}</a>{% endfor %}

```

---

<div class="post-metadata">

**Author:** ![Kevin-C](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kevin-c/32/34570_2.png) [@Kevin-C](https://community.hubspot.com/u/Kevin-C)\
**Post date:** [January 6, 2020, 5:57pm UTC](https://community.hubspot.com/t/converting-datetimeformat-for-stringtotime/17886/4 "2020-01-06T17:57:30Z")

</div>

Awesome!

Yeah filters can be very confusing. Not too long ago I made the exact same mistake you did haha so I’m very happy to help out.

The retrospective is my favorite part of development. You will almost always find a better more efficient wat to handle your logic!

---

<div class="post-metadata">

**Author:** ![Dhemes](https://avatars.discourse-cdn.com/v4/letter/d/5daacb/32.png) [@Dhemes](https://community.hubspot.com/u/Dhemes)\
**Post date:** [December 14, 2022, 7:11am UTC](https://community.hubspot.com/t/converting-datetimeformat-for-stringtotime/17886/5 "2022-12-14T07:11:35Z")

</div>

Using DateFormat. format() method  
Get the date to be converted.  
Create an instance of SimpleDateFormat class to format the string representation of the date object.  
Get the date using the Calendar object.  
Convert the given date into a string using format() method.  
Print the result.

Regards,

Will
