0

I have a special time format from json, ex: 07_23 means 07:00 - 23:00,and when it shows 24, ex: 07_24 it should change to 07:00 - 23:59.

My code is here:

$(document).ready(function () {
  var json = [{"name":"Meee","time":"07_24"}];
  
  for (var i = 0; i < json.length; i++) {
    tr = $('<tr/>');
    tr.append("<td>" + json[i].name + "</td>");
    tr.append("<td>" + json[i].time + ":00" + "</td>");
    $('table').append(tr);
    
    $("td:contains('_')").text($("td:contains('_')").text().replace('_', ':00-'));
    $("td:contains('24')").text($("td:contains('24')").text().replace('24:00', '23:59'));
    
  }
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table>
    <tr>
        <th>Name</th>
        <th>Time</th>
    </tr>
</table>

JSFiddle

I think my code is a little bit stupid, are there any best way to remove 07_24 underline, add :00 for each number, and change 24 to 23:59? Or can I use regular expression to achieve that goal?

5
  • Just to be clear, there is nothing wrong with your above code correct? Commented Nov 11, 2015 at 3:49
  • Yes, there's nothing wrong with my code, but I think it's not the best way to achieve my goal. Commented Nov 11, 2015 at 3:51
  • Personally I think your method is fine. I don't think it's a "stupid" way to do it. Commented Nov 11, 2015 at 3:57
  • Why the hell is everybody there downvoted? Commented Nov 11, 2015 at 4:11
  • I don't know why... it shocks me @@ Commented Nov 11, 2015 at 5:39

4 Answers 4

1

as @Spencer Wieczorek mentioned in comment .. your code is good .. but If you need to simplify it a little bit .. or just need to know another way to doing that

$(document).ready(function () {
        var json = [{"name":"Meee","time":"07_24"}];

        for (var i = 0; i < json.length; i++) {
            var Splittime = json[i].time.split('_');
            var all_time = Splittime[0]+':00-'+((Splittime[1] == 24)? '23:59' : Splittime[1]+':00');
            tr = $('<tr/>');
            tr.append("<td>" + json[i].name + "</td><td>" + all_time + "</td>");
            $('table').append(tr);
        }
    });

Working Demo another Demo

Sign up to request clarification or add additional context in comments.

Comments

0

http://jsfiddle.net/xfdqj6oo/2/

var formatTime = function(time) {
    return time.split('_').map(function(hour) {
        return hour === '24'? '23:59': hour + ':00';
    }).join('-');
}

$(document).ready(function () {
    var json = [{"name":"Meee","time":"07_24"}];

    for (var i = 0; i < json.length; i++) {
        tr = $('<tr/>');
        tr.append("<td>" + json[i].name + "</td>");
        tr.append("<td>" + formatTime(json[i].time) + "</td>");
        $('table').append(tr);            
    }
}); 

Basically there is a function to take care of the time formatting/transformation.

Comments

0

You can write a pretty simple regular expression to do this:

var new_string = "some string 07_23 bla bla".replace(/([0-2][0-9])_24/g,"$1:00-23:59").replace(/([0-2][0-9])_([0-2][0-9])/g,"$1:00-$2:00");

which outputs "bla bla some string 07:00-23:00 bla bla"

Comments

0

You change always the same part so use regular expression is not necessary. Because you just have to change the _ to :00- and the 24:00 to 23:59 it's constant so you dont need regular expression.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.