Unix 时间格式总是 returns “50 年前”
Unix time formatting always returns "50 years ago"
我正在使用 Hacker news API. I want to format Unix time to be like "35 minutes ago, 1 hour ago" etc. I am using javascript-time-ago 库。该值始终为 50 years ago
这是一个 API 回复:
{
"by" : "dhouston",
"descendants" : 71,
"id" : 8863,
"kids" : [ 8952, 9224, 8917, 8884, 8887, 8943, 8869, 8958, 9005, 9671, 8940, 9067, 8908, 9055, 8865, 8881, 8872, 8873, 8955, 10403, 8903, 8928, 9125, 8998, 8901, 8902, 8907, 8894, 8878, 8870, 8980, 8934, 8876 ],
"score" : 111,
"time" : 1175714200,
"title" : "My YC app: Dropbox - Throw away your USB drive",
"type" : "story",
"url" : "http://www.getdropbox.com/u/2/screencast.html"
}
这是我的组件:
// @Flow
import React from 'react';
import TimeAgo from 'javascript-time-ago'
import en from 'javascript-time-ago/locale/en'
....other imports ....
type PropsT = {
id: number,
by: string,
kids: Array<number>,
score: number,
url: string,
title: string,
time: number | Date
}
const ListItem = (props: PropsT) => {
const {by, kids = [], score, url, title, id, time} = props;
const site = getSiteHostname(url) || 'news.ycombinator.com';
const link = getArticleLink({url, id});
const commentUrl = `${ITEM}${id}`;
const userUrl = `${USER}${by}`;
TimeAgo.addLocale(en)
const timeAgo = new TimeAgo('en-US');
const formatedDate = timeAgo.format(time)
return (
<Item>
<TitleLink href={link} target="_blank">
{title}<Host>({site})</Host>
</TitleLink>
<Description>
{score} points by
<Link href={userUrl} target="_blank">
{by}
</Link>
{formatedDate} ago
{ ' | '}
<Link href={commentUrl} target="_blank">
{kids.length} comments
</Link>
</Description>
</Item>
);
}
export default ListItem;
结果总是50年前如何正确格式化?
我实际上需要将 time
乘以 1000。这解决了问题:
const formatedDate = timeAgo.format(time * 1000)
我正在使用 Hacker news API. I want to format Unix time to be like "35 minutes ago, 1 hour ago" etc. I am using javascript-time-ago 库。该值始终为 50 years ago
这是一个 API 回复:
{
"by" : "dhouston",
"descendants" : 71,
"id" : 8863,
"kids" : [ 8952, 9224, 8917, 8884, 8887, 8943, 8869, 8958, 9005, 9671, 8940, 9067, 8908, 9055, 8865, 8881, 8872, 8873, 8955, 10403, 8903, 8928, 9125, 8998, 8901, 8902, 8907, 8894, 8878, 8870, 8980, 8934, 8876 ],
"score" : 111,
"time" : 1175714200,
"title" : "My YC app: Dropbox - Throw away your USB drive",
"type" : "story",
"url" : "http://www.getdropbox.com/u/2/screencast.html"
}
这是我的组件:
// @Flow
import React from 'react';
import TimeAgo from 'javascript-time-ago'
import en from 'javascript-time-ago/locale/en'
....other imports ....
type PropsT = {
id: number,
by: string,
kids: Array<number>,
score: number,
url: string,
title: string,
time: number | Date
}
const ListItem = (props: PropsT) => {
const {by, kids = [], score, url, title, id, time} = props;
const site = getSiteHostname(url) || 'news.ycombinator.com';
const link = getArticleLink({url, id});
const commentUrl = `${ITEM}${id}`;
const userUrl = `${USER}${by}`;
TimeAgo.addLocale(en)
const timeAgo = new TimeAgo('en-US');
const formatedDate = timeAgo.format(time)
return (
<Item>
<TitleLink href={link} target="_blank">
{title}<Host>({site})</Host>
</TitleLink>
<Description>
{score} points by
<Link href={userUrl} target="_blank">
{by}
</Link>
{formatedDate} ago
{ ' | '}
<Link href={commentUrl} target="_blank">
{kids.length} comments
</Link>
</Description>
</Item>
);
}
export default ListItem;
结果总是50年前如何正确格式化?
我实际上需要将 time
乘以 1000。这解决了问题:
const formatedDate = timeAgo.format(time * 1000)