2

Im quite new to the Twitter API so I was hoping to have some guidance regarding my question please see a snippet of my code here : https://jsfiddle.net/amiguel0687/zbakwqt3/.

$(function(){

  $('#submit').on('click',function(){
      user = $('#user').val();
      $.ajax ({
            type:"POST",
            url: "request.php",
            data: { user : user },
            success: function(data) {
              
              results = $.parseJSON(data);
              $.each(results, function(key, value) {
              tweet_interval = value.created_at;
              tweet_details = value.text;
              tweet_times = new Date(Date.parse(tweet_interval));
              tweets = tweet_times+"\n"+tweet_details; 
              console.log(tweets);

            });
              

            }
        });
  });

});

Now here is the value returned from the API request

Twitter API created_at:

Wed Feb 22 16:11:13 +0000 2017

Using the javascript Date Function() it turns into this

Wed Feb 22 2017 16:20:02 GMT+0800 (China Standard Time)

it automatically gets the timezone where I'm from which would make the result when converted into (Hours,Minutes,Seconds AM/PM) all wrong. Is there a way to have the values just convert into a 12-Hour format and display showing the time it was created like in the Twitter user's timeline. I was hoping for a javascript/jquery solution only but If I need something else entirely then I will have a look at it as well.

UPDATE: According to twitter the value should be 8:20 AM I'm using PCGamer(@pcgamer) as my reference.

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
Red Miguel
  • 31
  • 6
  • Relevant SO [here](http://stackoverflow.com/questions/8945029/converting-date-to-gmt-0) – Alexander Nied Feb 22 '17 at 16:57
  • Not sure about this it seems to be wrong. So setting GMT to 000 would give the correct value? forgot to mention that the correct value should be 8:20 AM – Red Miguel Feb 22 '17 at 17:06
  • 1
    I can recommend momentjs.com if you intend to do any non basic date manipulation. – powerbuoy Feb 22 '17 at 17:12
  • Regarding MomentJS -- It's true-- as much as I hate adding dependencies, moment.js goes a long way towards simplifying date interactions and smoothing out differences between browsers. – Alexander Nied Feb 22 '17 at 18:01
  • @anied yes I've been trying momentJS specifically duration but the time is still off and the minute is showing as 1 digit. – Red Miguel Feb 22 '17 at 18:15
  • @powerbuoy do you have a fiddle i can reference to? – Red Miguel Feb 22 '17 at 18:15

1 Answers1

0

I figured out the solution it was not as complicated as I thought it would be. Apparently you have to set your timezone manually(I was under the impression it would do this automatically since it's online) in twitter in order for it to match your/server timezone.(My Bad it was my first time using Twitter). After updating the settings on my profile. It was already pulling the correct time using my code. I'm leaving this here just in case someone encounters a similar issue/problem.

Red Miguel
  • 31
  • 6