AngularJS not passing date data to web API

Greetings! I am currently working on a web application using AngularJS. I have a date value in AngularJS, for example 13-10-2017. In C#, I have the following field:

 public DateTime LicenseExpiryDate { get; set; }

When I send 13-10-2017 in an AJAX request, LicenseExpiryDate is being accepted as 0001-01-01. Can someone please advise me on how to resolve this issue? I've noticed that my Angular date is in dd-mm-yy format, while C# date uses the default format yyyy-mm-dd.

I attempted to convert it to yyyy-mm-dd using the following function:

 function formatDateDOsetyymmdd(date) {
                debugger;
                var d = new Date(date),
                    month = '' + (d.getMonth() + 1),
                    day = '' + d.getDate(),
                    year = d.getFullYear();

                if (month.length < 2) month = '0' + month;
                if (day.length < 2) day = '0' + day;

                return [year, month, day].join('-');
            }

However, when I test this function, it returns NaN-NaN-NaN. Any assistance would be greatly appreciated. Thank you!

Answer №1

If you receive a string in the format dd-mm-yyyy as input, your task is to convert that string into three separate numbers and then utilize them in the new Date function.

function formatDateStringToNumbers(date) {
    date = date.split('-').map(Number);
    var d = new Date(date[2], date[1] - 1, date[0]),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();
    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;
    return [year,month, day].join('-');
}
console.log(formatDateStringToNumbers('13-10-2017'));

Answer №2

If you find it challenging to perform this task using JavaScript, consider utilizing moment.js.

For more information, visit: https://momentjs.com/

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

Tips for executing embedded scripts within templates

I am using a controller to display the Instagram embedded code <div class="instagram_here" [innerHtml]="instagram_embeded_code"></div> However, it is only showing a blank Instagram block. https://i.stack.imgur.com/gNPDL.png I suspect there m ...

When working with JSON data in AngularJS, remember to utilize the ng-repeat directive to

I am working with a JSON file that looks like this: { "2014-08-01T23:00:00.000+05:30": { "In": 12, "Out": 23, "s1": 0 } "2014-08-01T22:00:00.000+06:00": { "In": 0, "Out": 0, "s2": 0 } "2014-08-01T22:00:00.000+06:30": { "In": 54, "Out": 0, "s3": 0 ...

JQuery method for extracting a specific span's content from a div

I am faced with extracting specific text from a span within a div element. Below is the code snippet for my Div: '<div class="dvDynamic_' + pid + '"><p hidden="true">'+pid+'</p><span class="count_' + pid ...

Avoiding duplication of prints in EJS template files

In my EJS code, I have created a loop to fetch the total amount of items from the database. Here is my current code: <h2>Summary</h2> <% if(typeof items.cart!=="undefined"){ var amount = 0; %> <% i ...

The effectiveness of a promise chain is consistent, even when the return statement is subject to conditions

After reorganizing this sequence, I am perplexed at how it continues to function regardless of a conditional return statement in one of the .then sections: function addList(name) { let listObj = {}; listObj.name = name; return nameExists(name) //returns a ...

I am looking for some guidance on utilizing the data obtained from a JSON response in a separate HTML file with AngularJS. Can

Upon user login, various data is sent back to different HTML pages that must be used in conjunction with a subsequent POST to a URL on another page. For example, after authentication, user information such as userid, sex, and other data are retrieved for u ...

Live events are not showing up upon page load, but they work flawlessly on the local server

I am currently developing a project using Laravel and Angular. In this project, I am implementing the angular ui-calendar as well as utilizing the jQuery context menu plugin to create dynamic menus for each event. The versions I am working with are fullca ...

Prevent page refresh when submitting a form using PureCSS while still keeping form validation intact

I'm currently implementing PureCSS forms into my web application and facing a challenge with maintaining the stylish form validation while preventing the page from reloading. I discovered that I can prevent the page reload by using onsubmit="return f ...

Building module dependencies in the Dojo dojo

I'm in the process of utilizing the Dojo builder to compile a unified file that encompasses all the necessary modules for my application, excluding the application itself. Below is an illustration of a layer definition: layers: { 'dojo/dojo ...

Why isn't Meteor.call functioning in the stub?

As I delve into the realm of async JavaScript coding, I have encountered a gist that has left me puzzled: https://gist.github.com/dariocravero/3922137 Specifically within client_save.file.js - there are parts of this code snippet that baffle me: fileRead ...

Intermittently play a series of sound files, with only the final sound ringing out

My goal is to create an app that plays a sound based on a number input. I have several short MP3 audio files for different numbers, and I want the app to play these sounds in sequence. However, when I try to do this, only the last sound corresponding to th ...

Issue with calling function from props in React is not being resolved

There seems to be an issue with the function not being called when passed into a functional component. While the onSubmit function is triggered, the login(email, password) function inside the Login component is never executed. Despite placing console.log s ...

Utilize the useState hook to update state when changes occur in the

I currently have a functional component that utilizes a useState hook. The values it holds are sourced from my redux store, and I aim to update the state with the new store state every time an action is dispatched. At the moment, I've manually set an ...

Discovering how to navigate to a link within a web table using Cypress has been a challenge, as I keep receiving the error message stating that the element is not visible due to its CSS property being

Trying to click on the first enabled link in the 'Action' column of a web table has been proving difficult. In the example given, the first two rows do not have an enabled link, so the goal is to click on '8.5 AccountH' https://i.stack ...

Having trouble getting Jquery Ajax Post to work properly when using JinJa Templating?

Objective: My goal is simple - to click a button and post information to a database. Problem: Unfortunately, clicking the button doesn't seem to be posting to the database as expected. Setup: I am working with Flask Framework, Jquery, and Jinja Temp ...

Customize the label and value in Material UI React Autocomplete

If you visit this link, you can see an example of what I'm trying to achieve. My goal is to have the option label and value be different from each other. In the provided example, the following code snippet is used: const defaultProps = { ...

Incorporating External HTML Content Using JQuery Function

Looking for assistance with a JQuery function: function addSomeHTML() { $("#mysection").html("<div id='myid'>some content here</div>"); } I am trying to have this part: <div id='myid ...

When rendering in React, the output of a multidimensional object may appear as undefined

Currently experiencing a challenge with a React application that I am developing. I am attempting to display data on the screen, however, there are instances where an object key is not available. For example: <div> <TableRow> <TableCel ...

How can I make a checkbox checked in AngularJS?

Hello, I am currently working on a web application using AngularJS. Within this application, I have a form where I am populating values into a multi-select dropdown. <li ng-repeat="p in locations"> <input type="checkbox" ng-checked="master" n ...

Using Express.js with Pug.js, dynamically render the page with new content following a fetch POST request

I have a collection of article previews sourced from a database and rendered on the webpage using a Pug.js mixin. each article in articles +articlePreview(article.title, article.text, article.imgSrc) To enhance the user experience, I want to implem ...