0

I have a table listing the dates field that are in 2015-08-27T22:38:21.0Z format. I managed to convert them into August 27th 2015, 3:38:21 using the moment() http://momentjs.com/. now i need to apply this changed format into my table.

html:

<table id="table">
    <thead>
        <tr>
            <th>Date</th>
            <th>Address</th>
            <th>Details</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="date-time-format">
                <time datetime="{{updatedDate}}">{{updatedDate}}</time>
            </td>
            <td>
                {{address}}
            </td>
            <td>
                {{details}}
            </td>
        </tr>
    </tbody>
</table>

here is the js:

$.each(data, function(time, format){
    var timeformat = format.date;
    moment(timeformat).format('MMMM Do YYYY, h:mm:ss');
});

Now how do i pass this:timeformat value into every td to return something like:

Date
September 13th 2015, 1:09:12
August 13th 2015, 1:09:12
October 13th 2015, 1:09:12
November 13th 2015, 1:09:12

fiddle:http://jsfiddle.net/hw6wvrgL/

Any help is greatly appreciated.Thanks!!!

user1234
  • 2,610
  • 2
  • 33
  • 73
  • possible duplicate of [How to format a JavaScript date](http://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – DPac Sep 21 '15 at 21:50
  • There's no code in your fiddle - and you should use a snippet. – Amit Sep 21 '15 at 21:58

2 Answers2

2

Just iterate over each time element:

$("time").each(function(){
    var date = $(this).text();
    var format_date = moment(date).format('MMMM Do YYYY, h:mm:ss');    

    $(this).text(format_date);
    $(this).attr("datetime", format_date);
});

JSFiddle

Alvaro Flaño Larrondo
  • 4,813
  • 2
  • 19
  • 39
0

A generic answer:

$("#table td.date-time-format time").map(function () {
  var originalTime = $(this).text("hello");
  $(this).text(yourTimeFormatFunctionGoesHere(originalTime));
});
Adrian Lynch
  • 7,501
  • 1
  • 25
  • 38