Transform JavaScript Date into EDM Date for OData Transformation

How can I convert a JavaScript date to the EDM Date format for OData usage?

let currentDate = new Date();
// Is there a native function to achieve this conversion?

The existing answers appear to be outdated, so I'm curious if the latest ECMAScript 2020 has a built-in method for this conversion.

Resources:

Important Note: This task is being performed within the Angular 10 framework.

However, when attempting to use the below function in Angular, an error was encountered:

function convertJSONDate(jsonDate, returnFormat) {
    var myDate = new Date(jsonDate.match(/\d+/)[0] * 1);
    myDate.add(4).hours();  //using {date.format.js} to add time and compensate for timezone offset
    return myDate.format(returnFormat); //using {date.format.js} plugin to format :: EDM FORMAT='yyyy-MM-ddTHH:mm:ss'
}

Error: Property 'add' does not exist on type 'Date'; Property 'format' does not exist on type 'Date'

Answer №1

I believe there may be some confusion with your question, but I can provide a solution based on my understanding of what you are trying to accomplish:

function convertJSONDateToEDM(jsonDate) {
  const data = /\d+/.exec(String(jsonDate));
  const time = data ? Number(data[0]) : 0;
  const date = new Date(time);
  const result = date.toISOString();
  return result;
};

console.log(
  convertJSONDateToEDM(`/Date(1609195194804)/`)
); // This will display "2020-12-28T22:39:54.804Z"

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

Saving functions in the localStorage API of HTML5: A step-by-step guide

I have encountered an issue while trying to store an array in local storage using JSON.stringify. The array contains functions (referred to as promises) within an object, but it seems that when I convert the array to a string, the functions are removed. Su ...

Having trouble fetching RSS XML data from external websites using the Axios and Vue.Js combination

I have been encountering an issue with utilizing the RSS XML feeds from multiple websites for my Web App. I attempted to incorporate the Axios function as shown below: axios.get('https://www.15min.lt/rss/sportas') .then((response) => { ...

Angular: Promise updating array but integer remains static

At the moment, I have a factory and a controller set up. The factory is responsible for updating with items retrieved from an endpoint along with the total number of pages of data. While my data array seems to be working properly, the pageCount (an integ ...

Switching focus between windows while working with React

My current setup involves using React. Every time I run yarn start, I can begin coding and see the changes in my browser. However, there's a recurring issue where my terminal keeps notifying me about errors in my code every 10 seconds or so. This cons ...

Unexpected issue: THREE js Object position does not update after rotation

For the past couple of weeks, I've been engrossed in creating a solar system model using Three.js. After following tutorials and documentation, I successfully created a working model of the solar system. However, I encountered an issue when trying to ...

Is there a way to safeguard against accidental modifications to MatTab without prior authorization?

I need to delay the changing of the MatTab until a confirmation is provided. I am using MatDialog for this confirmation. The problem is that the tab switches before the user clicks "Yes" on the confirmation dialog. For instance, when I try to switch from ...

Issue with Webpack - receiving a 'TypeError' regarding the node module and the 'replace' function not being recognized

After reviewing similar answers like this one, I'm still feeling lost and hoping for some guidance. I recently updated the bootstrap theme on my flask app, and now I'm encountering an error during the docker build process at the yard run command. ...

`Cursor in ACE Editor jumping to start of the line`

Having a strange issue with my editor. Every time I type something, it automatically gets reversed because the cursor keeps jumping back to the beginning of the line. For example, if I type 'Say this', it appears as: :siht ekil puS On pressin ...

Retrieve data from a table row and update the information in a textbox upon clicking the edit button, then save the changes to

I am currently working with a table that contains data values stored in my database <?php try { $pdo = new PDO('mysql:host=localhost:3306;dbname=insulation;', 'root', 'admin'); $pdo->setAttribute(PDO::ATTR_ERRMO ...

What is the best way to retrieve the previous value of a reactive form?

I currently have a form set up with the following structure: this.filterForm = this.fb.group({ type: [null, [Validators.required]]}); As I monitor any changes that occur, I use the code snippet below: this.filterForm.controls["type"].valueChanges. ...

Issue: potentially harmful data utilized in a URL resource setting

Looking for some assistance - I'm attempting to embed a YouTube video onto my HTML page, but encountered an error message that reads: "Error: unsafe value used in a resource URL context." Upon reviewing my code, everything appears to be correct, and ...

When the state changes, the dialogue triggers an animation

Currently, I am utilizing Redux along with material-ui in my project. I have been experimenting with running a Dialog featuring <Slide direction="up"/> animation by leveraging the attribute called TransitionComponent. The state value emai ...

The unit test is running successfully on the local environment, but it is failing on Jenkins with the error code TS2339, stating that the property 'toBeTruthy' is not recognized on the type 'Assertion'

I've been tackling a project in Angular and recently encountered an issue. Running 'npm run test' locally shows that my tests are passing without any problems. it('should create', () => { expect(component).toBeTruthy();}); How ...

Having both forms and submit buttons on a single HTML page

<form id = "msform" action = "" method = "POST"> <input type = "text" name = "name" /> <input type = "submit" name = "submit1" value "Submit" /> </form> <form id = "msform" action = "" method = "POST"> <b>Name:</b> ...

Are <ng-template> and <ng-content> simply Angular directives, or are they unique Angular elements?

During my study of the Angular course, the instructor mentioned that ng-template is a directive. However, Angular actually has three types of directives: attribute, structural, and component. This has left me feeling confused about what exactly ng-templa ...

Lack of element content in AngularJS unit testing

I am currently working with AngularJS 1.5+, jasmine, and karma, and I am facing an issue when testing a component along with its template. When the component's template is compiled, it seems to be missing the expected content. Here is a snippet of th ...

Encountering a "Element is not defined" error in Nuxt when trying to render Editor.js and receiving

I've been working on creating an editor using Editor.js within my Nuxt project, but it seems like the editor isn't initializing properly when I render the page. import EditorJS from '@editorjs/editorjs'; interface IEditor { editor: E ...

Node.js function showing incomplete behavior despite the use of Async/Await

I am completely lost trying to understand where I am going wrong with the Async/Await concept. Below is my Node.js code split into two separate files. The problem I am facing is that the line console.log("hasInvoiceAlreadyBeenPaid:", hasInvoiceA ...

Utilizing React to implement a search functionality with pagination and Material UI styling for

My current project involves retrieving a list of data and searching for a title name from a series of todos Here is the prototype I have developed: https://codesandbox.io/s/silly-firefly-7oe25 In the demo, you can observe two working cases in App.js & ...

Tips for handling datetime in angular

Currently, I am working with Angular (v5) and facing an issue related to Datetime manipulation. I am trying to retrieve the current time and store it in a variable. After that, I need to subtract a specified number of hours (either 8 hours or just 1 hour) ...