0

I have a json feed with date and time in this format published 2016-09-26T02:18:40.000Z and I want to remove the time T02:18:40.000Z from showing.

this is the code

var feedUrl = 'var feedUrl = 'http://www.myfeed.com'

GetFeed(feedUrl , BuildCreative ); //get feeds with feed parser url and callback function


//callback for feedGetter
function BuildCreative(data){
    console.log(data);
    for(var i = 0; i < 4; i++){

        var split_date = 



        document.body.innerHTML += "<div class='feed-content'><a href="+ data[i].link +" target='_blank'><div id='imgHolder'><img src='" + data[i].image.replace('w=98' , 'w=140') + "'></img></div><p>"+ data[i].title  +"</p></a><span class='feed-date'>"+ data[i].published + "</span></div>";
    }
}

//Get feeds function partly based on jquery
function GetFeed(url , callback ){var request = new XMLHttpRequest();request.open('GET', url, true);request.onload = function() {if (request.status >= 200 && request.status < 400) {var data = JSON.parse(request.responseText);callback(data);} else {console.log('error getting feeds');}};request.onerror = function(e) {console.log(e);};request.send();}
Gupta
  • 8,882
  • 4
  • 49
  • 59
Joseph Zammit
  • 381
  • 2
  • 12

3 Answers3

1

You can create a Date object from your string and then format it's output rather easily.

var date = new Date("2016-09-26T02:18:40.000Z");

var dateString = [
  date.getUTCFullYear() , 
  ("0" + (date.getUTCMonth()+1)).slice(-2), 
  ("0" + date.getUTCDate()).slice(-2)
].join("-");
JuniorDev
  • 1,170
  • 2
  • 9
  • 19
  • Thanks you gave me the idea of using slice. – Joseph Zammit Sep 26 '16 at 07:58
  • 1
    @JosephZammit Your date string contains a timezone value, directly splicing it in half can result horribly in different browsers having different timezones. – JuniorDev Sep 26 '16 at 08:03
  • Ok noted i'll see how i go with this first – Joseph Zammit Sep 26 '16 at 08:11
  • if you take this approach and construct a Date object, you can use `.toDateString()` to output a readable date value, compatible with any locale https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString - highly recommended. – Squiggle Sep 26 '16 at 08:15
0

If you use moment.js try this

.startOf('day')

For more information see this post and this documantation page

If you use pure js look at this post The main idea is get date and set seconds, hours and other to zero:

d.setHours(0, 0, 0, 0);
Community
  • 1
  • 1
user3272018
  • 2,309
  • 5
  • 26
  • 48
0

I achieved the result I wanted by using slice

document.body.innerHTML += "<div class='feed-content'><a href="+data[i].link +" target='_blank'><div id='imgHolder'><img src='" + data[i].image.replace('w=98' , 'w=140') + "'></img></div><p>"+ data[i].title  +"</p></a><span class='feed-date'>"+ data[i].published.slice('0' , '10')  + "</span></div>";
Joseph Zammit
  • 381
  • 2
  • 12