JSON Scraping - Convert military time to standard time via Javascript

后端 未结 5 1966
终归单人心
终归单人心 2021-01-18 19:41

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.

<
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-18 20:16

    "Military time" (a.k.a. 24-hour time) is easily converted to 12-hour time via a simple modulo 12.

    JSFiddle example:

    var obj = {
        SaturdayClose: "21:00",
        SaturdayOpen: "10:00",
        SundayClose: "12:00",
        SundayOpen: "18:00",
        WeekdayClose: "21:00",
        WeekdayOpen: "10:00"
    }, prop, $output = $('#output'), time, x, meridiem;
    
    for (prop in obj) {
        if (obj.hasOwnProperty(prop)) {
            x =+obj[prop].substr(0, 2);
    
            if (x > 12) {
                x = x % 12;
                meridiem = "pm";
            } else {
                meridiem = "am";
            }
    
            time = x + ":00" + meridiem;
    
            $output.append("
  • " + prop + " " + time + "
  • "); } }

提交回复
热议问题