更改angularjs中的日期格式

时间:2017-06-20 06:24:48

标签: angularjs

我得到的日期格式如此/Date(1497683045200)/
我想将其转换为dd/MM/yyyy。我试过这个

 <td>{{data.From |  date:'MM/dd/yyyy' }}</td>

但格式没有改变。有什么建议吗?

2 个答案:

答案 0 :(得分:1)

创建一个像这样的自定义过滤器

class func isConnectedToNetwork() -> Bool {

    var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
    zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
    zeroAddress.sin_family = sa_family_t(AF_INET)

    let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
        SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, UnsafePointer($0))
    }

    var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
    if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
        return false
    }

    let isReachable = flags == .Reachable
    let needsConnection = flags == .ConnectionRequired

    return isReachable && !needsConnection

}

像这样称呼它

.filter('jsonDate',function(){
  return function(date){
    return new Date(date.match(/\d+/)[0] * 1);
  }
})

答案 1 :(得分:1)

试试这个

formatDate(date) {
        let d = new Date(date),
            month = '' + (d.getMonth() + 1),
            day = '' + d.getDate(),
            year = d.getFullYear();
        if (month.length < 2) month = '0' + month;
        if (day.length < 2) day = '0' + day;
        return [ month, day, year].join('/');
    }

在HTML上调用该自定义方法

<td>{{formatDate(data.From)}}</td>