JavaScript code to display Twitter created_at as xxxx ago

前端 未结 8 570
一生所求
一生所求 2020-12-24 14:51

I need some JS code that will take the created_at value from a Twitter feed and display it as xxxx ago.

I can find examples of creating the xxxx a

8条回答
  •  北荒
    北荒 (楼主)
    2020-12-24 15:38

    import * as luxon from 'luxon';
    
    // https://stackoverflow.com/a/20478182/5932012
    const TWITTER_DATE_FORMAT = 'EEE MMM d HH:mm:ss ZZZ yyyy';
    
    export const parseTwitterDate = (dateStr: string): luxon.DateTime =>
        luxon.DateTime.fromString(dateStr, TWITTER_DATE_FORMAT);
    
    export const formatTwitterDate = (dateTime: luxon.DateTime): string =>
        dateTime.toFormat(TWITTER_DATE_FORMAT);
    

提交回复
热议问题