Obtaining the date without the time in TypeScript and MongoDB

I'm working with TypeScript and have the following code snippet:

const EmployeeDetailsSchema: mongoose.Schema = new mongoose.Schema({
  employeeId: { type: String },
  advance: {
    lastAdvanceClosedOn: { type: String },
    pending: { type: String },
    nextVacation: { type: String },
    IquamaStatus: { type: String },
    allowedAdvance: { type: Number },
    duration: { type: String },
    requiredAdvance: { type: Number },
    adDate: { type: Date, default: new Date(new Date().getFullYear(),new Date().getMonth() , new Date().getDate()) }}
}
);

When I check my database table, the value of adDate looks like this:

adDate : 2020-03-12T00:00:00.000+00:00

My desired output format for adDate is:

adDate : 12-03-2020

Is there a way to achieve this without changing the 'type' to String?

Answer №1

try implementing moment.js library

moment(<place your specific date here>).format("DD-MM-YYYY")

Next, utilize the formatted value

Answer №2

It is important to keep your data source as the original Date format and avoid modifying it. However, when displaying it on the web or in a CLI, you will need to convert it to a string for any format other than integer. In MongoDB, dates are stored as Date but represented as integers, which count the milliseconds since EPOCH.

It's worth noting that @vishnu-shenoys response is outdated, as momentjs is no longer maintained and should be avoided. Instead, consider using date-fns's format function.

You can also utilize native JavaScript to extract the day, month, and year from the date using the built-in Date type in JavaScript.

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

Converting a string to a number in an Angular template

Within my select box, I am capturing the selected value and binding it to the variable startingYear. However, I need the type of startingYear to be a number, but it is currently registering as a string. Is there a way to convert it to a number? console ...

The web application encountered an error: "Uncaught TypeError: Cannot read property 'map' of undefined" when trying to retrieve

Struggling with a simple API fetch, and despite checking everything multiple times, I can't seem to figure out what's going wrong. It feels like I'm missing something crucial. import { useState } from "react"; import ProductCard fr ...

Techniques for importing esm libraries without requiring the "type": "module" declaration in the package.json configuration file

I have successfully implemented a TypeScript Node project but now I am facing an issue while trying to integrate the VineJS library into the project. The problem arises because VineJS is exclusively an ESM (ECMAScript Module) package, and when adding it to ...

Using the data of multiple locations within a for loop to create Leaflet markers for use in a click event

I am currently working on a leaflet map that showcases markers for the top cities in a selected country. The locationList array consists of objects with city information such as latitude, longitude, and cityName. These values are utilized to place markers ...

Testing asynchronous functions with Mocha

Currently, I am in the process of developing a node wrapper to interface with an external api. One particular challenge I am facing is testing the asynchronous functionality of the createJob method. Provided below is the test case code: api_key = "test_0d ...

Having issues with starting npm, can anyone assist me?

I encountered a problem today and tried using commands like npm install, but I couldn't find a solution. Can anyone help me out? The issue started when I pressed ctrl-c and then used npm start to reload the server, and that's when all these error ...

Efficiently adding values to a variable with the forEach method

I'm encountering an issue with displaying data from a JSON file similar to the one below: Currently, "checked" is set to null. I aim to update it to true within the steps array using a forEach loop. JSON data snippet: { "id": 4, "process": { ...

Unable to establish the origin for an element using JavaScript

Currently, I am developing a simple game in JavaScript that includes a grid and various cells. Here is the current look of the game which functions perfectly. However, I am facing an issue with setting the margin to 0 and wanting to center the canvas inste ...

Present information using Vue.js

Struggling to display just the name from the request object in my form using JavaScript. I'm new to working with JS and need some guidance. I attempted to use {{ request.name }}, but it's not functioning as expected. When I tried {{request}}, it ...

What could be causing the malfunction of my Nextjs Route Interception Modal?

I'm currently exploring a different approach to integrating route interception into my Nextjs test application, loosely following this tutorial. Utilizing the Nextjs app router, I have successfully set up parallel routing and now aiming to incorporate ...

Ways to navigate to a routerlink in Angular 2 without passing any parameters

Struggling with accessing a routerlink in Angular 2 without its parameters. The goal is to use the routerlinks to determine whether or not to display a specific element in the navigation. For normal routerlinks without parameters, I do it like this: *ngIf ...

Inserting additional information and assigning a category following a prosperous AJAX request accompanied by output from php echo

I'm currently working on implementing an AJAX call to my PHP file to send an email once a contact form is submitted. Initially, I had everything functioning properly where the response from PHP was displayed in a div above the form. However, I wanted ...

Steps to sending a request with a custom user agent

In my Angular app, I have successfully implemented server-side pre-rendering with the condition that it will only pre-render if a search bot is sending the request. Now, I need to verify if everything is pre-rendered correctly. However, when I visit my w ...

Get Angular events in the proper order

I am currently facing challenges with event handling in AngularJs. In my service, I send out events using the following line of code : $rootScope.$emit("FNH."+this.status, flowNodeHelper); When receiving the event in service "S1," I handle it as follows ...

What is the method for assigning a different property type in a mongoose schema other than the one that is specified?

Looking for some advice on setting up a user profile schema in Mongoose for a forum. My goal is to have the user's forum title as an ObjectID referencing the Title schema. I have already established this part of the setup. However, my dilemma is that ...

404 error occurs when attempting to load SVG files in Webpack 2

Recently, I delved into the world of webpack. However, I encountered a problem when trying to load an SVG file referenced in one of my CSS files. Despite trying various loaders such as this, that, and the other, I keep receiving a 404 error. Can anyone pro ...

Send a response from socket.io to the client's browser

I am working on a project where I need to retrieve the ID of the current drawer from the server and pass it to the client. Here is the code I have so far: Client Side: socket.emit('returnDrawer'); socket.on('returnDrawer', fu ...

Disappearances of sliding jQuery divs

I am currently working on a website using jQuery, and I have implemented a slide in and out div to display share buttons. The issue I am facing is that the code works perfectly on the first page, but on every other page, the div slides out momentarily and ...

Is there a way to achieve a transparent background while animating the second text?

I am seeking to create a unique typography animation that involves animating the second text, which is colored and consists of multiple text elements to animate. The animation should showcase each text element appearing and disappearing one after the other ...

Retrieving detailed information from MongoDB in a Node.js environment

I am currently developing a nodejs server using mongoDB. I have some sample data that looks like this: { "_id": ObjectId("555f1c0c7f4b820758b439b0"), "user": "Guest1", "friend": [{ "myfriend": "Guest2", "in ...