JSON Scraping - 通过 Javascript 将军事时间转换为标准时间
我正在从 url 中抓取 JSON 数据。时间是军用时间,我想知道在客户端检索后是否有办法将其转换为标准时间。
这是 JSON:
[
{
SaturdayClose: "21:00",
SaturdayOpen: "10:00",
SundayClose: "12:00",
SundayOpen: "18:00",
WeekdayClose: "21:00",
WeekdayOpen: "10:00"
}
]
这是我的 index.html:
<p>
Sun: ${ SundayOpen }a - ${ SundayClose }p Mon - Sat: ${ SaturdayOpen }a ${ SaturdayClose }p
</p>
这会返回这种类型的丑陋:
Sun: 18:00a - 12:00p Mon - Sat: 10:00a 21:00p
我宁愿返回这个:
Sun: 6:00a - 12:p 周一至周六:10:00a - 9:00p
I am scraping JSON data from a url. The time is military time and I was wondering if there is a way once I retrieve on the client side to convert it to standard time.
Here is the JSON:
[
{
SaturdayClose: "21:00",
SaturdayOpen: "10:00",
SundayClose: "12:00",
SundayOpen: "18:00",
WeekdayClose: "21:00",
WeekdayOpen: "10:00"
}
]
Here is my index.html:
<p>
Sun: ${ SundayOpen }a - ${ SundayClose }p Mon - Sat: ${ SaturdayOpen }a ${ SaturdayClose }p
</p>
This returns this type of ugliness:
Sun: 18:00a - 12:00p Mon - Sat: 10:00a 21:00p
I would rather return this:
Sun: 6:00a - 12:p Mon - Sat: 10:00a - 9:00p
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用日期脚本当然可以。如果您需要的只是将 24 小时制 转换为 12 小时制,您只需减去时间并添加所示的期间。
编辑
我添加了两次作为测试,
00:30
,应该是12:30 am
,和12:15< /code>,应该是
12:15 pm
。请参阅下面的新编辑。http://jsfiddle.net/tqXCL/3/
转换后将为您提供以下内容:
Using a date script will work of course. If all you need is to convert from 24 hour clock to 12 hour, you can simply subtract the time and add the period as indicated.
EDIT
I added two times as a test,
00:30
, which should be12:30 am
, and12:15
, which should be12:15 pm
. See the new edit below.http://jsfiddle.net/tqXCL/3/
Which gives you the following after conversion:
如果你想使用 html 而不是 json,你可以这样做。
请注意,这假设您已将 dateEl 设置为适当的元素,并且该元素不包含您不想转换的其他时间。
If you want to use the html, not the json, you can do this.
Note that this assumes you've set dateEl to the appropriate element, and that that element does not contain other times that you don't want to convert.
看看 date.js。它充满了方便的日期转换功能。
Take a look as date.js. It is full of handy date conversion functions.
演示
DEMO
“军用时间”(又名 24 小时制时间)可以通过简单的模 12 轻松转换为 12 小时制时间。
JSFiddle示例:
"Military time" (a.k.a. 24-hour time) is easily converted to 12-hour time via a simple modulo 12.
JSFiddle example: