unix time to date

북마크 추가

unix time 을 date format으로 변환 하는 방법 입니다. 

 

1. Date.format 설정

 

Date.prototype.format = function(f) {

    if (!this.valueOf()) return " ";

 

    var weekName = ["일요일", "월요일", "화요일", "수요일", "목요일", "금요일", "토요일"];

    var d = this;

     

    return f.replace(/(yyyy|yy|MM|dd|E|hh|mm|ss|a\/p)/gi, function($1) {

        switch ($1) {

            case "yyyy": return d.getFullYear();

            case "yy": return (d.getFullYear() % 1000).zf(2);

            case "MM": return (d.getMonth() + 1).zf(2);

            case "dd": return d.getDate().zf(2);

            case "E": return weekName[d.getDay()];

            case "HH": return d.getHours().zf(2);

            case "hh": return ((h = d.getHours() % 12) ? h : 12).zf(2);

            case "mm": return d.getMinutes().zf(2);

            case "ss": return d.getSeconds().zf(2);

            case "a/p": return d.getHours() < 12 ? "오전" : "오후";

            default: return $1;

        }

    });

};

 

String.prototype.string = function(len){var s = '', i = 0; while (i++ < len) { s += this; } return s;};

String.prototype.zf = function(len){return "0".string(len - this.length) + this;};

Number.prototype.zf = function(len){return this.toString().zf(len);};

 

 

2. 사용

 

 

$(window).ready(function(){

var unixTime = 1478755914;

var date =new Date(unixTime * 1000);  // unix time에 곱하기 1000 해줍니다

        var result = date.format("yyyy-MM-dd  HH:mm:ss");

});

 

 

3.예제

 

https://trandent.com/jsTest/16401620174541 


HKH
2017-01-23 17:01
SHARE
keyboard_arrow_left
keyboard_arrow_up