Storing the current date() in JSON format using AngularJS

I am currently working on saving various data, including the current date, in a JSON file stored in LocalStorage. While I have been successful in saving the data, the date is saved in the ISO 8601 format:

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

This format complicates things for me when I need to retrieve and filter the data later on. Is there a way to change the date format (such as converting it to DD-MM-YYYY) before parsing it into the JSON file?

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

Any suggestions or advice you can provide would be greatly appreciated!

Answer №1

You should consider using Moment JS as it could provide a solution to your issue.

For more information on momentJS, visit their website at http://momentjs.com/.

Answer №2

If you need to display the current date, you can achieve this by using the following code snippet:

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

Answer №3

To make it easier for you to parse, my recommendation is to convert the date into a string format. Many APIs utilize this method to store current date and time effectively. For instance, if you want your date/time format to be:

month date year hr min sec 

You can then convert it to a timestamp using Java's conversion methods. For example:

If your date is 13-13-1992 and time is 7:48:78, your date string will be 726909318. You also have the flexibility to convert it into any other preferred timestamp. Check out this link for more information.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to verify if an item is enabled

To ensure a button in my angular application is enabled, I am using Protractor for testing. Here is the test script: it('submit should not be enabled',function() { var price = by.name('price'), oldCategory = by.name ...

What is the best way to merge two approaches for tallying items within each category?

I have an Angular 8 application that includes two methods for displaying the number of items in each category. These items are retrieved from the back-end and are categorized as follows: <mat-tab> <ng-template mat-tab-label> ...

Import several image files using AngularJS

I recently came across a helpful tutorial that demonstrates how to upload pictures from your local computer using AngularJS directives. As a newcomer to Angular, I followed the instructions but now I'm stuck on modifying it to display the selected ima ...

Does JavaScript array filtering and mapping result in a comma between each entry in the array?

The code snippet above showcases a function that retrieves data from a JSON array and appends it onto a webpage inside table elements. //define a function to fetch process status and set icon URL function setServerProcessesServer1761() { var url = "Serv ...

Creating a bar chart in Chart JS using an array of data objects

I need to create a unique visualization using a bar chart where each bar represents a user or student. Each bar will have an xAxis label displaying the student's name. The code below is a VueJS computed property named chartData For my bar chart data ...

There was a parsing error due to encountering an unexpected reserved word 'interface' in the code, as flagged

I'm encountering an issue with my code when trying to utilize Props. The error message I'm receiving is "Parsing error: Unexpected reserved word 'interface'. (3:0)eslint". This project is being developed using next with TypeScript. Er ...

I keep encountering a 'Missing Permissions' issue whenever I attempt to execute the ban command in Discord.js. What could be causing this problem?

While working on coding a ban command for my Discord server, I encountered an issue where the command was not working as expected. Upon testing, I received an error message on the console stating DiscordAPIError[50013]: Missing Permissions. This was puzzli ...

How can one restrict the display of fields in the Meteor aldeed tabular package?

How can I restrict certain data from being displayed in an aldeed tabular datatable? For instance, if my collection includes attributes A, B, C, D and attribute C contains sensitive information that should not be published, is there a way to prevent it fro ...

Utilizing the onFocus event in Material UI for a select input: A comprehensive guide

Having trouble adding an onFocus event with a select input in Material UI? When I try to add it, an error occurs. The select input field does not focus properly when using the onFocus event, although it works fine with other types of input. Check out this ...

Keep track of the user's email address as they complete the form

I currently use a Google Form to gather information from employees who work in remote locations Emp No * Punch * Customer details / mode or travel All the data collected is stored in a Google spreadsheet structured as follows: Timestamp Emp No Punch ...

Upload a user-sent image to a remote SFTP server for safekeeping

Can someone help me figure out how to upload a file to an SFTP remote server using ssh2-sftp-client? I am trying to retrieve the file from the user via a post request along with the destination. To process the file, I am utilizing multer. const Client = r ...

Integrating HTML, JavaScript, PHP, and MySQL to enhance website functionality

Exploring the intricacies of HTML, JavaScript, PHP, and MySQL, I have been working on an order form to understand their interactions. View Order Form The aim of this table is to allow users to input a quantity for a product and have JavaScript automatica ...

Arrays in Vue Data

After creating an array and pushing data into it, the array turns into a proxy, preventing me from using JavaScript array functions on it. export default { name: 'Home', components: { PokeList, FilterType, SearchPokemon}, data() { r ...

Elements that allow for asynchronous data submission without requiring a traditional submit button

Hey there, I could really use some help with this puzzle I'm trying to solve. Here's the situation: <ul> <li>Number: <span id="Number">123</span></li> </ul> I want to set up a connection between t ...

Using jQuery to arrange information from an API into a table

Currently, I am in the process of learning jQuery as it is a new concept for me. I have attempted to make a request to an example API and received an array of objects that need to be listed in a table. However, I am facing difficulty in sorting it within t ...

Arranging Angular Array-like Objects

I am in possession of an item: { "200737212": { "style": { "make": { "id": 200001510, "name": "Jeep", "niceName": "jeep" }, "model": { "id": "Jeep_Cherokee", "name": "Cherokee", "nice ...

Store the value of $scope.data in a variable within the same controller

Here is the issue with my code: $scope.data = {}; var init = function () { $http.post('views/Report.aspx/GetMailReport', {}) .success(function (data, status, headers, config) { $scope.data ...

Is it possible to use a += operator with attr() and val() functions in JavaScript?

Ever since I switched to jQuery, my development process has significantly sped up. However, there is one task that has become a bit more time-consuming: appending a string to an attribute or value. For instance, if you wanted to add a letter to the value ...

GET request body parameters are not defined

Can anyone help me with retrieving the parameters of a GET request? I've tried various methods but everything keeps logging out as 'undefined': GET // Looking for a list of Players $scope.find = function() { $scope.players = Players.qu ...

Creating a stylish button using a combination of CSS and Javascript classes within a webpage layout

Is it feasible to include a button in the layout that has HTML, CSS styles, and JavaScript functionality? For instance: This button is designed with both CSS styling and JavaScript classes. How can one incorporate CSS and JavaScript along with HTML conte ...