Deciphering dates in JavaScript

var formattedDate = new Date(parseInt(thisObj.Patient.DateOfBirth.substr(6)));

After checking the output of my Date Object, I see the following result.

Wed May 04 2011 09:30:00 GMT+0530 (GMT+05:30)

I am wondering how I can split May, 04, and 2011 into separate variables like

var Month = May;
var Date = 04;
var Year = 2011;

In addition, I would like to know how to determine if the person's Age is less than one year old or not.

Answer №1

Make the most of the object's various useful methods.

Check out Date Object resources

  • getDay(): provides the day of the week (from 0-6)
  • getFullYear(): fetches the year (four digits)
  • getSeconds(): retrieves the seconds part of a date (0-59)

Answer №2

To access the numerical values, you must handle the formatting yourself. Utilize Date.getFullYear(), Date.getMonth() and more. Check out the documentation of this object at https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date.

Comparing dates is possible by subtracting them:

alert(new Date() - formattedDate);

This calculation will reveal the milliseconds between the current date and formattedDate. You just need to determine the number of milliseconds in a year.

Answer №3

Consider implementing the following:

formattedTime.getHour() .getMinute() .getSecond()

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

Modify an individual attribute in a local JSON file using a put request in Angular 8

I am looking to make updates to my JSON file located in the assets folder. Specifically, if I want to update just one property of my JSON object, I would like it to only affect that particular property without impacting any others: For example, in the sam ...

Adding dynamic attributes like pattern and title to input controls

I currently have an HTML input field that looks like this: <input type="text" value="<%= this.CustomerAcctNumber %>" name="CustomerAcctNumber" id="CustomerAcctNumber" maxlength="19" onkeyup="CustomerAcctNumberChange()" required > Upon loading ...

Using jQuery to assign a value within a c:foreach loop

I am currently working on a project that requires fetching values via an ajax request using a specific id. Subsequently, I need to retrieve the JSON List returned and inject it into a jstl foreach loop. Here is the snippet of code for the ajax GET request: ...

Passing components as props in Vue routing

Looking to extract a number from a route parameter : routes: [{ path: '/user/:id', component: User }] Wanting to access the prop within the User component: export default { props: { id: Number } } Encountering an error message: I ...

Determine the SVG arc connecting two given points

My situation involves having coordinates for points X and A, A',... as follows: X [x,y] (starting point) A [a,b], A'[a',b'], etc. (ending points) I also have information about angles XCA, XCA', etc. However, I am struggling t ...

Importing and exporting JavaScript libraries

In this scenario, I have a collection of libraries that I've defined. One such library is lib1.js: export default { Func1() { return ... } } (and similarly for my other libraries in the same folder) Now, I am importing these librar ...

ReactJS state refuses to update

In my FreeCodeCamp leaderboard table, I have implemented functionality where clicking on the highlighted table header calls different URLs based on sorting criteria. The application either calls https://fcctop100.herokuapp.com/api/fccusers/top/recent or ht ...

Can you please explain the differences between "resolved" and "rejected" in a deferred object within jQuery?

Recently, I inquired about a refreshing page solution if an internet connection is available on Stack Overflow. The user @Fabrizio Calderan provided an elegant approach utilizing deferred object implementation: setInterval(function() { $.when( ...

Why does obj.attr('id') give undefined while obj.getAttribute('id') returns a value?

Why does obj.getAttribute('...') work for me in my JavaScript code, but not obj.attr('...') and obj.prop('...')? I am confused about this issue. Can someone please explain it to me? function ShowTips(obj) // show tip in elem ...

Tips for retaining the value of a variable when the page is reloaded

I need to store the value of the variable loggedIn, as it determines the behavior of a function in appComponent.html. Can you explain how I can achieve this? Template of app component: <li class="nav-item"> <a class ...

Convert a JavaScript array into a PHP database, retrieve it, and convert it back into a JavaScript

Hey, I have a JavaScript array that I need to convert into a MySQL database, and then I want to retrieve it as a JavaScript array. var bookmarks = new Array(); bookmarks[0] = new Array(); bookmarks[1] = new Array(); bookmarks[2] = new Array(); // Add y ...

Events for validation on the client side and user interface blocking

Lately, we have implemented HTML 5 client-side validation in our forms. A unique situation arose during this process. To prevent multiple form submissions and give it a sleek appearance, we added an overlay with a loading indicator when the form is being ...

Updating JSON data in real time using JavaScript by manipulating MySQL database entries

My database has a mysql structure consisting of the columns ID, NAME, and TYPE. The data is then converted into a JSON format as shown below: [ {id: "1", name: "Snatch", type: "crime"}, {id: "2", name: "Witches of Eastwick", type: "comedy"}, { ...

Shadows on menu buttons transform when clicked using React and CSS

I'm currently working on customizing the styling of a menu using CSS in a project that involves the use of "react-horizontal-scrolling-menu". While I've been successful in styling the menu items with .menu-item:hover & .menu-item:active, I am ...

Incorporate a new CSS class into a DIV using JavaScript

Sample HTML: <div id="bar" class="style_one"></div> Is there a way to include the class style_two without deleting style_one? final outcome: <div id="bar" class="style_one style_two"></div> ...

Having trouble transferring data from an aspx file to the code behind using $.ajax({ type: "POST", with Visual Studio 2017 C#?

Utilizing web-forms to gather data from a form and then transmit the data to the code-behind in order to forward it to an API. By clicking on a button, I trigger a JavaScript function that gathers the data and then sends it over to my aspx.cs file for comm ...

The Material UI export of 'useInsertionEffect' (imported as 'useInsertionEffect$1') was not located within the 'react' library while utilizing the Appbar component

https://i.sstatic.net/twJwh.png 1: https://i.sstatic.net/Y2Zdo.png**strong text**https://i.sstatic.net/twJwh.png While attempting to implement the Appbar component from Material UI, I encountered an unexpected error. ...

Adding data to the SharePoint RichText Control using the WinForms WebBrowser Control

I am struggling to modify the content of an SP Rich Text field using the WinForms web browser control. While changing the values of other controls is straightforward, I have encountered difficulties with Rich Text fields. I found some suggestions on this b ...

Sending data from TextBoxFor to controller with @Ajax.ActionLink in MVC

I’ve gone through numerous questions dealing with the same issue, yet none of them seem to solve my problem (or I’m completely missing the point). As the title suggests, I am attempting to transfer the value from a TextBoxFor to my controller using an ...

Adding a div inside an iframe

Can someone help me troubleshoot this code snippet? jQuery('<iframe id="groundplan_popup" class="groundplan_hidden" />').appendTo("#popup_holder"); var iframe = jQuery("#groundplan_popup"); iframe.att ...