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

The function passed to the modal is not functioning correctly when stored within the state

Is it possible to create a unique modal that can be utilized for multiple actions on the same page? To achieve this, I have implemented a state to store the title, description, and submit function for modal click and modal open state. In addition, I want t ...

Utilize the Webstorm debugger in conjunction with node.js for seamless debugging

Several weeks ago, I attempted to get the webstorm debugger up and running, but unfortunately, it didn't work. Now, I'm giving it another shot, but I'm faced with the same outcome. I am following the instructions outlined here: http://www.j ...

What is the best way to navigate back to the top of the page once a link has been clicked?

One issue I'm facing is that whenever I click on a link in NextJS, it directs me to the middle of the page: <Link href={`/products/${id}`} key={id}> <a> {/* other components */} </a> </Link> I believe the problem l ...

Angular and Express: Automatically redirecting to the previous page after user login

A question similar in nature can be found here, although not directly relevant to my situation. Within my Single Page Application, I am implementing PassportJS for user authentication. There are multiple routes that necessitate user login. The routing is ...

The loading of script files is not working properly in an AngularJS web API application

After developing an application in angularjs that utilizes angular routing for calling web api services, everything functions properly when run from visual studio. However, upon deploying the application on an IIS web server, the script files fail to load ...

Submitting a form using jQuery and processing the response

Can a form be submitted using jQuery without utilizing json, ajax, or other methods for handling the result? For example: <form id="loginform"> //some input fields and a submit button. </form> And then with jQuery: $("#loginform").sub ...

Restangular does not consider query parameters in its operations

Currently, I am utilizing Restangular in my front-end application and attempting to query the following URL: http://localhost:4000/study?projectId=123456 This URL should return a list of studies associated with a specific project. I have implemented the ...

Can Comet be implemented without utilizing PrototypeJs?

Can Comet be implemented without utilizing PrototypeJs? ...

Universal PM2 Configuration Settings in JSON Format

My current process involves initiating numerous Node.js processes using pm2. These processes are configured in a JSON file and started by executing pm2 start pm2.json Upon examining the JSON file provided below, it's evident that there is significan ...

Having trouble getting JQuery Ajax POST to work in Codeigniter

Encountering an issue with jQuery AJAX post in CodeIgniter, where clicking the submit button triggers an error. Below is the code snippet: home.js form.on('submit', function(e){ e.preventDefault(); var fields = {}; form.find(' ...

Why does the combination of "minus, space, minus" result in the "plus" operation?

When running the command 'node -e 'console.log(- -1)' it outputs 1, which is expected. However: When executing the command 'node -e 'console.log(1 - - 1)' it displays 2, which puzzles me. It seems like when subtracting a ne ...

Discover the complete guide on incorporating a JavaScript library with additional dependencies in Angular 2

I am a beginner with angular 2 and I am attempting to integrate the Miso Dataset JavaScript library into my angular 2 project. The Miso library requires other JavaScript libraries as dependencies. Although I have included all the necessary JavaScript file ...

The designated apiUser.json file could not be located within the _http.get() function

It's puzzling why my URL isn't working in _http.get('app/api/apiUsers') for Angular version 4.0.0, when it functions correctly in Angular version 2.3.1. The code is identical in both Angular versions: import { Injectable } from ' ...

Unable to execute findOneAndUpdate as a function

I've been researching all morning and testing different solutions, but I'm still unable to resolve this issue. Every time I run the code below, I receive the error "TypeError: req.user.findOneAndUpdate is not a function": req.user.findOneAndUpd ...

Enhancing productivity with tools for developers and effortless tab navigation

During my development process, I always keep the developer tools open on one or more of my tabs. However, I noticed that when I switch to a tab where the developer tools were not previously open, a resize event is triggered. Strangely, this event causes el ...

Converting a JS result set into JSON format

Having an issue with converting code from XML output to a JSON object instead. Here is the current code: public String evaluate() throws Exception { // Code block here } I need assistance in using GSON JSON utility methods to convert the result s ...

Changes made to Objective C variables within an Async block do not affect the value of the Class variable

I am facing an issue with using my parsed JSON values from an async block in different methods and in the viewDidLoad function. In the async block, it returns the value properly, but in other methods, it just returns null. Below is a snippet of code from ...

Element on webpage "Expands" When Scrolled into Visibility

After posting a question on Stack Overflow, I noticed discrepancies between the element dimensions reported by Chrome Inspector and Selenium WebDriver. While Chrome Inspector showed w = 979, h = 1961, Selenium returned dimensions of 979 and 1461 respective ...

Is there a way to update the color of a different AngularJS material button when a user selects one button?

I have two buttons on the screen. When the user clicks button 1, an HTML form is displayed. At that point, I want to change the color of the other button to an AngularJS Material button (Button). I've tried some examples but haven't been successf ...

A Node.js function may not provide a response immediately due to a pending request

Here is a simple upload method using node.js and express.js: upload: function(req, res, next){ // Loop through each uploaded file async.each(req.files.upload, function(file, cb) { async.auto({ // Create new path and unique file ...