Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用angularJs过滤器从json解析日期字段_Javascript_Json_Angularjs - Fatal编程技术网

Javascript 如何使用angularJs过滤器从json解析日期字段

Javascript 如何使用angularJs过滤器从json解析日期字段,javascript,json,angularjs,Javascript,Json,Angularjs,下面是我从json文件/Date(1435837792000+0000)/ 我需要以以下格式显示日期2010年10月29日上午9:10:23Javascript中的日期原型上没有类似于format()的函数。但有以下几种方法: getDate() // Returns the date getMonth() // Returns the month getFullYear() // Returns the year getHours() // Returns the hour getMinute

下面是我从json文件
/Date(1435837792000+0000)/


我需要以以下格式显示日期
2010年10月29日上午9:10:23

Javascript中的日期原型上没有类似于format()的函数。但有以下几种方法:

getDate() // Returns the date
getMonth() // Returns the month
getFullYear() // Returns the year
getHours() // Returns the hour
getMinutes() // Returns the minute
getSeconds() // Returns the second
您可以使用以下方法自己形成字符串:

function formatDate(dt) {
    var monthNames = ["January", "February", "March", "April",
        "May", "June", "July", "August", "September",
        "October", "November", "December"];

    var h = dt.getHours(), am;
    if (h > 12) {
        am = 'pm';
        h = h - 12;
    } else {
        am = 'am';
    }

    return monthNames[dt.getMonth()].substring(0, 3) + ' ' +
        dt.getDate() + ', ' + dt.getFullYear() + ' ' +
        h + ':' + dt.getMinutes() + ':' + dt.getSeconds() + ' ' +
        am;
}

console.log(formatDate(new Date(1435837792000+0000)));
如果您发现自己需要更多的日期解析和格式,请查看库。利用这一时刻,您可以:

moment(new Date(1435837792000+0000)).format("MMM D, YYYY hh:mm:ss A")

您可以从json中提取该值

将其分配给控制器中的范围变量。 yourCtrl.js

json = { name: 'stackoverflow',
         date: 1435837792000+0000
       };
$scope.today = new Date(json.date); //This converts the date value from the json to a healthy looking date something like this `2015-07-02T11:49:52.000Z`
today.html

{{ today | date:'medium' }}
此角度过滤器将以所需的日期格式显示您的日期。 2010年10月29日上午9:10:23

编辑:

然后使用如下的角度过滤器进行管道连接


{today | date:'mmmd,yhh:mm:ss'}
{{today | date:'medium'}}
根据您的需求。

首先获取JSON到变量的时间戳,下面是一个简单的例子:

然后,和的混合,既古怪(但本地化),也可能不可靠的非标准,可能会接近:

date = date.toDateString() + ' ' + date.toLocaleTimeString('en');
alert( date === 'Wed Jul 01 2015 3:37:29 PM' );

还有一个函数分别返回2015年7月2日星期四11:49:52 GMT(RFC 1123)。

使用JS将其转换为日期对象,然后再转换为日期函数

<html>
<head>
<script>
function jsonDatetoJSDate(){
    var dateFromServer = "/Date(1435837792000+0000)/";
    //Javascript conversion
    var prsdDt = new Date(parseInt(dateFromServer.substr(6)));
    //getting Date Object
    var uDate = new Date(prsdDt);
    alert(uDate);
}
</script>
</head>
<body onload="javascript:jsonDatetoJSDate()">
    Use Date Function on javaScript
</body>
</html>

函数jsonDatetoJSDate(){
var dateFromServer=“/Date(1435837792000+0000)/”;
//Javascript转换
var prsdDt=新日期(parseInt(dateFromServer.substr(6));
//获取日期对象
var uDate=新日期(prsdDt);
警惕(uDate);
}
在javaScript上使用日期函数

正如您在评论中提到的,如果您得到的JSON是

var obj = {
              "PublishDate": "\/Date(1435757849000+0000)\/"
          }
您无法控制更改值(因为您将日期()括在斜杠周围),因此需要

  • 转义日期前后的斜杠

      obj.PublishDate.replace(/\//g, "");
    
  • 并计算剩余的表达式

      eval(obj.PublishDate.replace(/\//g, ""))
    

要获取实际日期:
Wed Jul 08 2015 11:48

您可以添加完整的json吗?=/Date(143583792000+0000)/不是有效的json。这是我得到的“PublishDate”:“\/Date(14357549000+0000)\/”@Hannes Johansson我知道使用angular={{1288323623006{Date:'Middle'}}:2010年10月29日上午9:10:23我们可以实现我得到的bt值假设是先过滤掉的我会说这是重复的,不是重复的,但无论如何都很有趣:。从json中提取后,我得到/Date(143583792000+0000)/它不是143583792000+000OK。你可以发布整个json吗?我使用了console.log(日期(1435837792000+0000))。输出为2015年7月8日星期三12:10:43 GMT+0530(印度标准时间)。只需执行:$scope.today=Date(1435837792000+0000);然后使用日期过滤器{{today{date:'medium'}}没有json像这样的“PublishDate”:“\/date(1435757849000+0000)\/”
  obj.PublishDate.replace(/\//g, "");
  eval(obj.PublishDate.replace(/\//g, ""))