Unable to use the toDateString() method on a JavaScript Date object

I've encountered an issue that I need help understanding. My goal is to convert a date into a more user-friendly format using the toDateString() method. However, I keep receiving an error message stating "toDateString() is not a function."

Currently, I am able to achieve this using toString():

truncateDate() {
    if (this.employee && this.employee.dob)
    {
        let birthDate = this.employee.dob;
        console.log(birthDate); // 2011-06-12T05:00:00.000Z Prints to console
        console.log(birthDate.toString());
    }
}

But when I try to use toDateString(), it results in an error:

truncateDate() {
    if (this.employee && this.employee.dob)
    {
        let birthDate = this.employee.dob;
        console.log(birthDate); // 2011-06-12T05:00:00.000Z Prints to console
        console.log(birthDate.toDateString());
    }
}

I'm unsure of what mistake I might be making here. Any insights would be appreciated.

Answer №1

To make use of the function, first convert the string to a Date Object. You can find more information on how to do this on MDN at this link.

this.user={}
this.user.birthdate='6/12/2020'
let dateOfBirth = this.user.birthdate;
console.log(dateOfBirth);
console.log(new Date(dateOfBirth).toDateString());

//

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

What methods are available for debugging JavaScript in a embedded browser within Windows?

I am currently facing an issue where I need to debug Javascript (Jquery) sources in an embedded browser on Windows 7 and XP while using Sketchup. I attempted using console.log, however it affected the layout of the app. Does anyone have a solution for th ...

React ensures that the page is not rerendered until after data has been fetched

I am dealing with the following code snippet. This is my React hook: const [isLoading, setIsLoading] = React.useState(true); useEffect(() => { setIsLoading(() => true); // I expect the page to rerender and display loading now. const select ...

The following authentication error occurred: JWEDecryptionFailed - the decryption process has encountered a failure

[...nextauth]/route.js file import { User } from "@/lib/models"; import { connectToDb } from "@/lib/utils"; import NextAuth from "next-auth"; import GitHubProvider from "next-auth/providers/github"; export const aut ...

When using the `const { }` syntax, which attribute is made accessible to the external

I am using the ngrx store as a reference by following this example: https://stackblitz.com/edit/angular-multiple-entities-in-same-state?file=src%2Fapp%2Fstate%2Freducers%2Fexample.reducer.ts Within the code in example.reducer.ts, there is this snippet: ...

Error: The function `res.status` is unsupported

I've been working on a function to allow uploading images to imgur through my express API (nodejs), but I'm running into an issue when calling a function that returns a promise: TypeError: res.status is not a function at uploadpicture.then T ...

Why is Visual Studio Code not highlighting my nested multi-line JavaScript comment as I anticipated?

/*/*! what is the reason */ for this annotation*/ Can someone explain why the annotation is not working as expected in this scenario? I've already verified it in VS code. ...

Update the ng-repeat attribute in HTML using either vanilla JavaScript or AngularJS

When the user clicks on the 'sort by book title' button, I want to change the ng-repeat="x in books' to ng-repeat="x in books|orderBy:'country'" in the HTML code. How can I achieve this action using JavaScript/Angular? Here is a ...

Incorporating a polling feature into an HTTP request within Angular 8

How can I implement a polling mechanism in order to fetch the status of a job (type Status) every minute for a service that requests this object with a specific JOB_ID as an argument? retrieveJobStatus$(JOB_ID): Observable<Status> { const url = ...

Encountering an "Invalid hook call error" while utilizing my custom library with styled-components

I recently developed my own custom UI tool using the styled-components library, integrating typescript and rollup for efficiency. One of the components I created looks like this: import styled from 'styled-components' export const MyUITest2 = s ...

Launching a Node.js script with pm2 and implementing a delay

Utilizing the amazing pm2 package to maintain the longevity of my node.js applications has been extremely helpful. However, I have encountered a problem that I am unsure how to resolve. One of my applications involves multiple scripts, a server, and sever ...

The mobile devices are not showing my HTML website

I have implemented the following CSS link code on my website: <link rel="stylesheet" href="index_files/front.css" media="all" type="text/css" > Additionally, I have included the following code <meta name="HandheldFriendly" content="True"> & ...

Passing public field names with typed expressions in TypeScript

I've been exploring ways to pass an array of property names (or field names) for a specific object without resorting to using what are often referred to as "magic strings" - as they can easily lead to typos! I'm essentially searching for somethin ...

Mastering the art of utilizing particles.js

Particles.js doesn't seem to be functioning properly for me—I'm struggling to pinpoint the issue. Any guidance or suggestions would be greatly welcomed, as I'm unsure whether it's related to an external dependency... HTML: <div ...

The process of HTML compilation is halted due to the unexpected presence of the forbidden 'null' data type, despite the fact that null cannot actually be a valid value in

Encountering an issue with my HTML code, where the compiler stops at: Type 'CustomItem[] | null | undefined' is not compatible with type 'CustomItem[] | undefined'. Type 'null' cannot be assigned to type 'CustomItem[] ...

NuxtJS (Vue) loop displaying inaccurate information

I have a dataset that includes multiple languages and their corresponding pages. export const myData = [ { id: 1, lang: "it", items: [ { id: 1, title: "IT Page1", }, { ...

Refresh the Data Displayed Based on the Information Received from the API

As someone who is relatively new to React, I have been making progress with my small app that utilizes React on the frontend and a .NET Core API on the server-side to provide data. However, I have encountered a problem that I've been grappling with fo ...

Error: The Rollup module is unable to locate the 'jquery' file

I've been facing an issue while trying to bundle a vuejs single file component using rollup. The error message that keeps popping up is Module not found: Error: Can't resolve 'jquery'. Despite spending countless hours on this problem, I ...

Creating a dynamic TypeScript signature that includes an optional argument

For some unknown reason, I am attempting to implement a reduce method on a subclass of Map: const nah = Symbol('not-an-arg'); class MapArray<A, B> extends Map<A, B> { reduce<T = [A, B]>(f: (prev: T, next: [A, B]) => any ...

How can I determine if my clients are utilizing the CDN or NPM versions of my JavaScript library?

At this moment, I'm contemplating releasing an open-source version of my library on NPM. My main concern is figuring out how to track the usage of my CDN or NPM by clients. Is there a method available to achieve this? ...

Applying reduce method for accessing object information within an array and transforming its structure

In my data structure, I have a list of deliveries that includes different cities and the number of units delivered to each city: var deliveries = [{ location: "Chicago", units: 10 }, { location: "San Francisco", units: 5 }, { location: ...