【发布时间】:2012-02-15 08:42:12
【问题描述】:
我有一个 Json 时间戳,我想使用 javascript 将其转换为简单的日期时间格式。
我需要以下格式的日期和时间:dd-mm-yyyy hr:mn
这是我希望提取时间戳的示例 json 日期:“timestamp”:1326439500
{
"count": 2,
"d": [
{
"title": "Apple iPhone 4S Sale Cancelled in Beijing Amid Chaos (Design You Trust)",
"description": "Advertise here with BSA Apple cancelled its scheduled sale of iPhone 4S in one of its stores in China’s capital Beijing on January 13. Crowds outside the store in the Sanlitun district were waiting on queues overnight. There were incidents of scuffle between shoppers and the store’s security staff when shoppers, hundreds of them, were told that the sales [...]Source : Design You TrustExplore : iPhone, iPhone 4, Phone",
"link": "http://wik.io/info/US/309201303",
"timestamp": 1326439500,
"image": null,
"embed": null,
"language": null,
"user": null,
"user_image": null,
"user_link": null,
"user_id": null,
"geo": null,
"source": "wikio",
"favicon": "http://wikio.com/favicon.ico",
"type": "blogs",
"domain": "wik.io",
"id": "2388575404943858468"
},
{
"title": "Apple to halt sales of iPhone 4S in China (Fame Dubai Blog)",
"description": "SHANGHAI – Apple Inc said on Friday it will stop selling its latest iPhone in its retail stores in Beijing and Shanghai to ensure the safety of its customers and employees. Go to SourceSource : Fame Dubai BlogExplore : iPhone, iPhone 4, Phone",
"link": "http://wik.io/info/US/309198933",
"timestamp": 1326439320,
"image": null,
"embed": null,
"language": null,
"user": null,
"user_image": null,
"user_link": null,
"user_id": null,
"geo": null,
"source": "wikio",
"favicon": "http://wikio.com/favicon.ico",
"type": "blogs",
"domain": "wik.io",
"id": "16209851193593872066"
}
]
}
【问题讨论】:
-
var date=new Date(timestamp)var hours = date.getHours(); // 时间戳的分钟部分 var minutes = date.getMinutes(); // 时间戳的秒部分 var seconds = date.getSeconds(); // 将以 10:30:23 格式显示时间 var formattedTime = hours + ':' + minutes + ':' + seconds;
-
@RajatSinghal 只会给出时间,不是吗?
-
您可以从 Date 对象中获取任何日期、年份...而不是 gethours 使用 getYear...
-
json 没有时间戳。它没有日期/时间的概念——它只是一个传输封装。该数字是生成 json 文本的任何语言的时间戳。
-
@RajatSinghal 谢谢它的帮助。
标签: javascript json datetime timestamp simpledateformat