FlackAP
FlackAP

Reputation: 21

Displaying Unix Time as MM/DD/YYYY in underscore template

I'm currently using instagram's API to gather JSON data about photos and I'm using an underscore template to display the data. I'm having difficulties printing the date (which is provided in Unix time) in a simple month/day/year format.

<div class="mask">  
    <h2> <!-- Formatted date --> </h2>  
    <p>
         <% if (item.get('caption')) { %>
         <%= item.get('caption').text %>
         <% } %>
    </p>  
    <p><%= item.get('likes').count %> likes, <%= item.get('comments').count%> comments</p>  
</div>  

Upvotes: 1

Views: 445

Answers (1)

FlackAP
FlackAP

Reputation: 21

Figured out what I needed to do! The date attribute was giving me a string, so after solving that using using Date() and parseInt() in conjunction with moment.js, I realized all I had to do was multiply the value by 1000 to give me the correct date. Hope this comes in handy for anyone using an API that provides dates as strings in unix time.

<h2> <%= moment(new Date(parseInt(item.get('created_time'))*1000)).format("MM-DD-YYYY")
%></h2>  

Upvotes: 1

Related Questions