简体   繁体   English

使用AngularJS在JSON中保存date()

[英]Saving date() in JSON with AngularJS

I'm trying to save the current date (among other data) in a JSON file in LocalStorage. 我正在尝试将当前日期(以及其他数据)保存在LocalStorage中的JSON文件中。 So far I get to save the data, but JSON will save it in the ISO 8601 format: 到目前为止,我已经保存了数据,但是JSON将以ISO 8601格式保存数据:

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

It kinda makes it hard for me later when I want to call the data back and filter it and so on. 当我想回叫数据并对其进行过滤等等时,这对我来说有点困难。 Is there some way to change the date() format (To DD-MM-YYYY, for instance) before parsing into the JSON file? 在解析为JSON文件之前,是否可以通过某种方法更改date()格式(例如,更改为DD-MM-YYYY)?

Here's my current code: 这是我当前的代码:

$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!");
    }
}

Thanks in advance for any advice you can trow in my direction! 预先感谢您的任何建议,您可以朝我的方向前进!

您可以使用

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

Try Moment JS, I think it will solve your problem. 试试Moment JS,我认为它将解决您的问题。

Info about momentJS can be found here :- http://momentjs.com/ . 约momentJS信息可以在这里找到: - http://momentjs.com/

I would suggest you to take the date in a converted string such that its eazy for you to get while parsing ,many of the APIs are also using these techniques to save the current date/time. 我建议您使用转换后的字符串中的日期,以便在解析时很容易获得它,许多API也在使用这些技术来保存当前日期/时间。 Eg : you wanted to take your date/time like 例如:您想将日期/时间记为

month date year hr min sec 

then you convert it to anytime stamp such that java provides conversion method from one time stamp to another. 然后将其转换为任何时间戳,以便java提供从一种时间戳到另一种时间戳的转换方法。

eg your date is 13-13-1992 and time is 7:48:78 then your date string will be 726909318 also you can convert to any other time stamp you prefer. 例如, your date is 13-13-1992 and time is 7:48:78 then your date string will be 726909318您还可以将其转换为您喜欢的任何其他时间戳。 Visit This 访问此

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

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