How can I convert Twitter API post time/date to valid ISO format?

bloppit

I'm building a twitter interface and I want to display the times like Twitter itself.

I perform a GET request and grab the tweet, I then pass it into moment.js in order to convert it.

Here is a code snippet of this:

client.get('direct_messages', {screen_name: 'theclearytheory', count: '3'}, function(error, messages, response){
    if(!error){
        var privateMessageTime = [];

        for(var i=0; i < messages.length; i++){
            console.log(messages[i].created_at);
            privateMessageTime.push(moment(messages[i].created_at).fromNow());
        }
    } else {
        console.log(error)
    }

Right now, this DOES work fine. If I get back Sun Oct 02 11:59:43 +0000 2016 then the program correctly turns this into: 2 months ago.

The problem is that I'm getting this warning message in the console:

Deprecation warning: value provided is not in a recognized ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info. Arguments: [0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: Fri Jul 22 10:19:18 +0000 2016, _f: undefined, _strict: undefined, _locale: [object Object]

I have scoured over the documentation it links to but I just can't work out how to work with this. If anyone has any experience with this (or is that much smarter than me), I'd really appreciate any help please!

VincenzoC

As stated in the parsing docs:

Warning: Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.

For consistent results parsing anything other than ISO 8601 strings, you should use String + Format

Since you are getting Sun Oct 02 11:59:43 +0000 2016 you will have:

moment(messages[i].created_at, 'ddd MMM DD HH:mm:ss Z YYYY').fromNow()

instead of moment(messages[i].created_at).fromNow().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JavaScript How Can I convert this innerText to a valid date format?

How i can convert ISO time format to yyyy-mm-dd hh:mm:ss using python

How can I convert multiple iso date format with the same function to date in java?

How can i convert this model to a valid RealmObject?

How do I format this string '8:00-9:00 AM' to a valid ISO time format?

Python Selenium - How can I get timedate inside of class?

How to convert ISO Date format to yyyymmddhsi format?

How I can reformat dates to ISO format in my code?

How can I convert it to Java lambda format?

How can I convert a date format in SQL?

How can I convert the format of time in the dataframe?

How can I post a GIF image to twitter using Twit library?

How can I convert a timestamp from yyyy-MM-ddThh:mm:ss:SSSZ format to MM/dd/yyyy hh:mm:ss.SSS format? From ISO8601 to UTC

How can I convert an XML date to a valid datetime?

Using Ember Data RestSerializer to POST to a 3rd-party API, how can I change the payload format?

Can I convert and replace unix times stored in a file with iso 8601 format?

How can I convert this date-format in a format accepted by lubridate?

How can I convert a date format to another date format in Swift?

How to convert ISO8601 format into milliseconds?

How to convert a date with timezone to Javascript ISO format?

how to Convert Date into ISO Date Format in javascript

How can I format an ISO 8601 date to a more readable format, using Javascript?

How to convert YouTube API duration (ISO 8601 duration in the format PT#M#S) to seconds

How can i use alamofire for post api

How can I return post API message?

Twitter API: How can I know the exactly hour a tweet was created?

How can I get a bigger Twitter Video size from the API?

How can I publish an image from url with Twitter API?

How can I save a valid image to Github using their API?