Transforming the present date into the format of yyyy/mm/dd

I am trying to obtain the current date, but I am struggling to format it in the desired way. Most methods that I have come across use - as separators, whereas I need them to be separated by /.

I attempted a manual approach like this:

const today = new Date();
let dd = today.getDate();

let mm = today.getMonth() + 1; 
const yyyy = today.getFullYear();
if(dd < 10) 
{
    dd = '0' + dd;
} 

if(mm < 10) 
{
    mm = '0' + mm;
} 

today = yyyy + '/' + mm + '/' + dd;

While this method works, it is messy and leads to type errors since the values are numbers and I am treating them as strings. Additionally, I tried:

const date = new Date().toISOString().split("T")[0];

This approach provides the date in the correct order but with - separating the values. Is there a solution available that will format the current date using / as separators?

Answer №1

Here is the solution

let currentDate = new Date().toISOString().split("T")[0].replaceAll("-", "/");

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

"React - encountering issues with state being undefined when passing child state up through parent components

I am currently navigating the world of react and have encountered a hurdle. I find myself facing difficulties in updating the parent component based on changes in the child state. I was able to pass the child state to the parent by linking the child's ...

The RxDB Angular2-cli error message. "Cannot assign a 'Promise<void>' to a 'Promise<any>' parameter."

https://i.sstatic.net/50vu6.png I've been grappling with getting RxDB to function properly in a fresh project I initiated using the Angular CLI. Here's my process: ng new <Projectname> After that, I installed RxDB by running: npm instal ...

The Server Components render encountered a glitch

Screenshot of the errorI am encountering a strange error only in the production environment. The lack of additional information leads me to believe it may be due to security measures put in place for production. Unfortunately, I have been unable to repli ...

import the JSONP file from the local directory and assign it to a variable on

My current setup involves a local webpage that is loading a large JSON database file by utilizing a file named data.jsonp that contains data={a JSON dictionary of information}. I then import this file in my HTML using <script src="data.jsonp"& ...

incorporating a dynamic parameter into the payload of an AJAX post request

I'm currently working on a function call where I want to extract the value of the variable data and place it in the data section of the function. However, despite my efforts, I haven't been successful so far. In PHP, I am attempting to retrieve t ...

The initial page in jqgrid may show rowobject value as undefined, but subsequent pages will display the correct rowObject values

While working with jqgrid-4.8, I encountered an issue where the rowObject value is coming up as undefined in the first page but values are received in subsequent pages when using rowObject.name. However, if rowObject[0] is used, the values of rowObject are ...

React app (storybook) experiencing loading issues with @font-face

I am struggling to load custom fonts from a local directory. Can someone provide assistance? Below is the code I am currently using: @font-face { font-family: 'My Custom Font'; src: url('./fonts/MyCustomFont.eot'); src: url(&apo ...

Incorporating external JavaScript libraries in Jint

I'm currently working on a new feature that requires the execution of user-defined, anonymous JavaScript functions retrieved from a database on the server side within an ASP.Net application environment. For this purpose, I am exploring the use of Jin ...

How can I detect click events on SVG path element using Angular?

Is it possible to detect click events on SVG elements such as path or g using Angular? To see an example of this, check out this demo. Right now, if a (click) event binding is added to the svg elements, the click() event handler will trigger. However, how ...

Is there a way to integrate a Control + F style text search box into a website using an iframe

My website has a lengthy iframe filled with text from another domain that I have no control over. When I need to search for specific text within this iframe, I typically press CTRL+F to locate and highlight the desired text. Previously, using CTRL+F was s ...

Locating the source and reason behind the [object ErrorEvent] being triggered

I'm facing an issue where one of my tests is failing and the log is not providing any useful information, apart from indicating which test failed... LoginComponent should display username & password error message and not call login when passed no ...

Executing a program on a website that records every single keystroke made while the webpage is active

Just to clarify, I am not developing a keylogger. I am working on a website for a video game where users can earn items by completing specific in-game actions, like typing something in the chat. Given the nature of this game, I believe the most effective ...

Is there a way to send an AJAX request to an MVC action from a JavaScript file?

In a file named job.js, there is code that works perfectly when run on localhost but results in a 404 error when run on an intranet server with an application name. Job.updateJob = function () { $.post('/Builder/ListJobItems', function (dat ...

Associating models using a many-to-many relationship in Sequelize unexpectedly restricts the ability to modify attributes in the join table

I am in the process of incorporating a voting feature into my app, allowing users to vote on responses from other users. The backend of my Node app is built using Express and Sequelize. Currently, I am utilizing a SQLite database for testing convenience. ...

Create a JavaScript function that continues to execute even after a button has been clicked

Although it may seem like simple logic, I am currently unable to log in. Imagine I have a function called mytimer() stored in a common file that is linked to every HTML page. mytimer(){...........................}; Now, at some point on another page, whe ...

Assigning null values, or initializing values in AngularJS

How can I clear input values to null after clicking on a button? Empty fields: https://i.sstatic.net/JDTZA.jpg Fields with existing values that I want to reset back to empty fields. https://i.sstatic.net/T5iKc.jpg HTML: <label class="item item ...

Is it possible to utilize Ionic for making an Ajax JQuery POST request?

As I try to implement NTLM authentication from my Ionic app, I have successfully tested the request using Postman. While Postman works with NTLM Authentication option, it does not offer any code for Angular (specifically for Ionic). In my research, I disc ...

Firebase's equalTo function seems to be malfunctioning

Having encountered an issue with Firebase, I am currently attempting to fetch all of my posts in JavaScript. Specifically, I am looking for posts in the correct language that are marked as "published" and sorted by their published date. In my Firebase dat ...

JavaScript method not properly sorting the last nested array

I am having trouble with sorting the last nested array using arr[i].sort(). Despite trying various methods such as FOR and WHILE loops along with different operators, I still cannot achieve the desired result. Can someone help me identify what I am doing w ...

Is it possible for a React blog to be included in search engine results?

As I work on building my blog using React, Node.js, Express, Sequelize, and other technologies, a question has arisen in my mind: Will search engines index my articles, or will only the homepage of my site be noticed? For instance, if I have an article ti ...