js时间格式转换

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    <style media="screen">
        h3{
            color:red;
        }
    </style>
    <script src="http://cdn.hcharts.cn/jquery/jquery-1.8.3.min.js"></script>
    </head>

    <body>
        <h1>时间戳转成格式化时间</h1>
        <input type="number" id='inp1'>
        <button id='btn1'>转化</button>
        <h3 id='one'></span>
        <hr><hr><hr><hr><hr><hr>
        <h1>格式化时间转化成时间戳</h1>
        <input type="text" id=inp2>
        <button id='btn2'>转化</button>
        <h3 id='two'></span>

        <script>
            $(function(){
                $('#btn1').click(function(event) {
                    var timeStemp1 = $('#inp1').val();
                    var formatTime = format(timeStemp1);
                    $('#one').text(formatTime);
                });

                $('#btn2').click(function(event) {
                    var matTime = $('#inp2').val();
                    var stringT=get_unix_time(matTime)
                    $('#two').text(stringT)
                });


                function format(shijianchuo)
                {
                    //shijianchuo是整数,否则要parseInt转换
                    var time = new Date(parseInt(shijianchuo));
                    var y = time.getFullYear();
                    var m = time.getMonth()+1;
                    var d = time.getDate();
                    var h = time.getHours();
                    var mm = time.getMinutes();
                    var s = time.getSeconds();
                    return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
                }
                function add0(m){return m<10?'0'+m:m }

                function get_unix_time(dateStr){
                    var newstr = dateStr.replace(/-/g,'/');
                    var date =  new Date(newstr);
                    var time_str = date.getTime().toString();
                    console.log(time_str);
                    return time_str

                }

            })
        </script>
    </body>
</html>

 

posted @ 2017-02-14 21:50  webbky  阅读(416)  评论(0编辑  收藏  举报