繁体   English   中英

使用AngularJS在JSON中保存date()

[英]Saving date() in JSON with AngularJS

我正在尝试将当前日期(以及其他数据)保存在LocalStorage中的JSON文件中。 到目前为止,我已经保存了数据,但是JSON将以ISO 8601格式保存数据:

[{"date":"2014-10-13T17:55:32.953Z"}] 

当我想回叫数据并对其进行过滤等等时,这对我来说有点困难。 在解析为JSON文件之前,是否可以通过某种方法更改date()格式(例如,更改为DD-MM-YYYY)?

这是我当前的代码:

$scope.dateHeader = new Date(); 

$scope.recordlist = extractRecordJSONFromLocalStorage();

$scope.addRecord = function () {
    $scope.recordlist.push({ date: $scope.dateHeader});

    jsonToRecordLocalStorage($scope.recordlist);
};

function extractRecordJSONFromLocalStorage() {
    return JSON.parse(localStorage.getItem("records")) || [

    ];
}
function jsonToRecordLocalStorage(recordlist) {
    var jsonRecordlist = angular.toJson(recordlist);

    if (jsonRecordlist != 'null') {
        localStorage.setItem("records", jsonRecordlist);
    } else {
        alert("Invalid JSON!");
    }
}

预先感谢您的任何建议,您可以朝我的方向前进!

您可以使用

$scope.dateHeader = $filter('date')(new Date(),'dd-MM-yyyy');

试试Moment JS,我认为它将解决您的问题。

约momentJS信息可以在这里找到: - http://momentjs.com/

我建议您使用转换后的字符串中的日期,以便在解析时很容易获得它,许多API也在使用这些技术来保存当前日期/时间。 例如:您想将日期/时间记为

month date year hr min sec 

然后将其转换为任何时间戳,以便java提供从一种时间戳到另一种时间戳的转换方法。

例如, your date is 13-13-1992 and time is 7:48:78 then your date string will be 726909318您还可以将其转换为您喜欢的任何其他时间戳。 访问此

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM