JSON Scraping - 通过Javascript将军事时间转换为标准时间 [英] JSON Scraping - Convert military time to standard time via Javascript

查看:124
本文介绍了JSON Scraping - 通过Javascript将军事时间转换为标准时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从一个网址上刮取JSON数据。时间是军事时间,我想知道是否有一种方法,一旦我在客户端检索到将其转换为标准时间。



这是JSON: p>

  [
{
SaturdayClose:21:00,
SaturdayOpen:10:00 ,
星期日关闭:12:00,
星期日开放:18:00,
WeekdayClose:21:00,
WeekdayOpen:10:00
}
]

这是我的index.html:

 < p> 
Sun:$ {SundayOpen} a - $ {SundayClose} p Mon - Sat:$ {SaturdayOpen} a $ {SaturdayClose} p
< / p>

这会返回这种类型的丑陋:



太阳:18:00a - 12:00p星期一至星期六:10:00a 21:00p



我宁愿退回:



太阳:6:00a - 12:p星期一至星期六:10:00a - 9:00p

解决方案

使用日期脚本当然会奏效。如果您需要的是将 24小时制转换为12小时,您可以简单地减去时间并添加指定的时间段。



编辑



我添加了两个时间作为测试, 00:30 ,应该是 12:30 am 12:15 ,应该是 12:15 pm 。查看下面的新编辑。

  var times = {
SaturdayClose:21:00,
星期六开:10:00,
星期日关闭:12:00,
星期日开放:18:00,
WeekdayOpen:10:00,
WeekdayClose: 21:00,
WeekendOpen:12:15,
WeekendClose:00:30
};

console.log(times);

for(var time in times){
var parts = times [time] .split(':'),
hour = parts [0],
minutes = parts [1];

if(hour> 12){
times [time] =(hour - 12)+':'+ minutes +'pm';
} else if(hour == 0){
times [time] = 12 +':'+ minutes +'am';
} else if(hour == 12){
times [time] + ='pm';
} else {
times [time] + ='am';
}
}

console.log(times);

http://jsfiddle.net/tqXCL/3/



转换后,您会收到以下内容:

 星期六关闭9:00 pm
星期六开放上午10:00
星期日关闭12:00 pm
SundayOpen 6:00 pm
Weekday关闭9:00 pm
WeekdayOpen10:00 am
WeekendClose12:30 am
WeekendOpen12:15 pm


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

解决方案

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 be 12:30 am, and 12:15, which should be 12:15 pm. See the new edit below.

var times = {
    SaturdayClose: "21:00",
    SaturdayOpen: "10:00",
    SundayClose: "12:00",
    SundayOpen: "18:00",
    WeekdayOpen: "10:00",
    WeekdayClose: "21:00",
    WeekendOpen: "12:15",
    WeekendClose: "00:30"
};

console.log(times);

for (var time in times) {
    var parts = times[time].split(':'),
        hour = parts[0],
        minutes = parts[1];

    if (hour > 12) {
        times[time] = (hour - 12) + ':' + minutes + ' pm';
    } else if (hour == 0) {
        times[time] = 12 + ':' + minutes + ' am';
    } else if (hour == 12) {
        times[time] += ' pm';
    } else {
        times[time] += ' am';
    }
}

console.log(times);

http://jsfiddle.net/tqXCL/3/

Which gives you the following after conversion:

SaturdayClose "9:00 pm" 
SaturdayOpen  "10:00 am"    
SundayClose   "12:00 pm"    
SundayOpen    "6:00 pm" 
WeekdayClose  "9:00 pm" 
WeekdayOpen   "10:00 am"    
WeekendClose  "12:30 am"    
WeekendOpen   "12:15 pm"

这篇关于JSON Scraping - 通过Javascript将军事时间转换为标准时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆