0

I try to change a value of an attribute "data-published" of every <a> element to one changed by romString function that is changing format of date. What am I doing wrong?

<script>


      $(function(){       

        var id='139540263@N06';
        var tag = document.getElementById('html').getAttribute("data-tag");


        // Flickr Photostream feed link.
        $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=" + id + "&tags="+ tag + "&lang=en-us&format=json&jsoncallback=?", 

            function(data){$.each(data.items, 

                function(i,item){


                    // Number of images to show.            
                    if(i < 4){

                    var newTaken=new Date(item.date_taken.replace("T"," ").replace("Z","").replace("-08:00","")).getTime();
                    alert(newTaken);
                    // Create images and append to div id flickr and wrap link around the image.
                    $("<img/>").attr("src", item.media.m.replace('_z', '_c')).appendTo("[data-tag=html]").wrap("<a href='" + item.media.m.replace('_z', '_c') + "' name='"+ item.link + "' title='" +  item.title + "'data-taken='" + newTaken + "'data-published='" + item.published.replace("T"," ").replace("Z","") + "'></a>");



                    }

                }); 

            }); 

        });



    </script> 

Now it alerts that newTaken is NaN. Do you know why?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150

2 Answers2

0

Why not just do

$('.gallery>a').each(function() {
  var dt = $(this).data('published');
  $(this).data('published',new Date(dt).getTime());
});

assuming a format of

$(function() {
  $(".dd").each(function() {
    var dt = $(this).data("published");
    $(this).data("published",new Date(dt).getTime());
    $(this).text($(this).data("published"));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="dd" data-published="2015-01-21 16:43:00"></div>
<div class="dd" data-published="2016-01-21 16:43:00"></div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

html appear malformed at

<a data-published="2016-01-20 11:15:49" data-taken="2010-01-20 03:15:49" title="3" name="flickr.com/photos/139540263@N06/24130487059/"; href="farm2.staticflickr.com/1665/…; <img src="farm2.staticflickr.com/1665/…; </a>

should not include ; at close of attribute value assignments ; missing closing " at attribute values.


Moved .getTime() to after close of new Date() ; instead of at last index of matched results of parts

function romString(dateAsString){
    var parts = dateAsString.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/);
    return new Date(parts[1],
                parts[2] - 1,
                parts[3],
                parts[4],
                parts[5],
                parts[6]).getTime();
}
$('.gallery a').each(function() {
    var value = $(this).attr('data-published');
    var new_value=romString(value);
    $(this).attr('data-published', new_value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div class="gallery">
<a data-published="2016-01-20 11:15:49" data-taken="2010-01-20 03:15:49" title="3" name="flickr.com/photos/139540263@N06/24130487059/" href="farm2.staticflickr.com/1665/…"> <img src="farm2.staticflickr.com/1665/…" /> </a>
  </div>

function romString(dateAsString){
    var parts = dateAsString.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/);
    return new Date(parts[1],
                parts[2] - 1,
                parts[3],
                parts[4],
                parts[5],
                parts[6]).getTime();
}
$('.gallery a').each(function() {
    var value = $(this).attr('data-published');
    var new_value=romString(value);
    $(this).attr('data-published', new_value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div class="gallery">
<a data-published="2016-01-20 11:15:49" data-taken="2010-01-20 03:15:49" title="3" name="flickr.com/photos/139540263@N06/24130487059/" href="farm2.staticflickr.com/1665/…"> <img src="farm2.staticflickr.com/1665/…" /> </a>
  </div>
guest271314
  • 1
  • 15
  • 104
  • 177