Instead of using colons, display the separation of hours, minutes, and seconds with underscores

Currently, I am utilizing moment.js to retrieve the present date and time with the intention of formatting it in the specific format below

'MMMM Do YYYY, h:mm:ss a'

Unfortunately, the problem arises as the delineation between hours, minutes, and seconds appears as an underscore instead of colons. This is how I have integrated it:

download() {
  var url = OFFERS_URL + '?';
  this.$axios.get(url, { responseType: 'blob' })
  .then(response => {
    var file = new Blob([response.data]);
    FileSaver.saveAs(file, 'Offers ' + moment().format('MMMM Do YYYY, h:mm:ss a') + '.xls');
  });
}

When the file is downloaded, the name displayed is as follows:

Offers April 20th 2021, 1_26_59 PM 

whereas it should actually be:

Offers April 20th 2021, 1:26:59 PM

Answer №1

If you want to include the date in your filename, you can use the following format:

moment().format('DD-YYYY-MM_hh-mm-ss-a')

IBM provides guidelines on file naming conventions for operating systems (refer to )

It is important that each file name be unique within its directory to ensure a distinct path in the system.

Key points to consider when naming files are:

  • A file name can have up to 255 characters and include letters, numbers, and underscores.
  • The OS recognizes uppercase and lowercase letters separately, so FILEA, FiLea, and filea would be considered different names even if in the same directory.
  • File names should be descriptive and meaningful for easy identification.
  • Directory names adhere to the same rules as file names.
  • Avoid using special characters that hold significance within the OS when naming files.

These special characters to avoid are:

 / \ " ' * ; - ? [ ] ( ) ~ ! $ { } &lt > # @ & | space tab newline

Answer №2

Feel free to customize the file name format as per your requirements

const current_date = moment().format('MMMM Do YYYY, h_mm_ss A');
console.log("Deals "+ current_date +".xlsx");

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

Can Node.js handle parsing this as JSON?

Attempting to parse a small API that returns somewhat invalid JSON. Trying the following approach: var request = require('request'), url = 'urlhere'; request({ url: url }, function(error, response, body) { ...

Implementing an All-Routes-Except-One CanActivate guard in Angular2

In order to group routes within a module, I am aware that it can be done like this: canActivate: [AuthGuard], children: [ { path: '', children: [ { path: 'crises', component: ManageCrisesComponent }, ...

The 'data' variable is not defined in the React custom hook Pagination

I am currently working with an API that shows music categories on a browser, and I'm attempting to create a custom pagination hook. However, I keep encountering an error stating "object is not iterable." I am new to custom hooks and would appreciate a ...

Unable to identify the element ID for the jQuery append operation

After attempting to dynamically append a textarea to a div using jQuery, I encountered an issue. Despite the code appearing to work fine, there seems to be a problem when trying to retrieve the width of the textarea using its id, as it returns null. This s ...

Tips for sending an icon as a prop in React components

I'm struggling to implement an icon as a prop while using props for "optionText" and "optionIcon". The optionText is working fine, but I'm facing issues with the OptionIcon. File where I'm creating props import Icon from ...

How can you assign a default value in a React select dropdown and dynamically add the remaining values?

I'm currently developing a React application that includes a form with a select dropdown feature. My goal is to have the 'uom' field value from the selected product record automatically set as the default option in the dropdown when the user ...

node js retrieves information from the request body

Hey there! I'm diving into the world of Node.js and JavaScript, but I've hit a roadblock. I'm trying to fetch data from a URL using node-fetch, then parse it as JSON. However, I keep running into the issue of getting 'undefined' in ...

XMLHttpRequest Refusing to Send Data

This snippet of code is crucial for the custom extension: let url = "https://mywebsite.com/data.php"; function sendRequest() { var client = new XMLHttpRequest(); client.open("POST", url, true); client.setRequestHeader("Content-Type", "text/pla ...

Invoker of middleware and stack functions for Express.js with a focus on capturing the response object

It appears that the expressjs app contains a stack of Layer object Arrays. What function is utilized to pass the I am curious about: When a request is sent from the http client, which function is called first and how are the stack array functions with mi ...

Different methods of transferring PHP post information to JavaScript

Is there a cleaner and more elegant way to pass posted PHP variables to JavaScript for manipulation, rather than using inline JavaScript? I currently have a simple PHP form that posts data to specific variables and then uses inline JavaScript to handle the ...

Is there a way to manipulate a button's toggle state using Javascript as the page loads?

I am in the process of developing a straightforward web application that allows users to customize their settings. On the settings page, there are several toggle buttons that need to have their state changed during the page load to align with the preferenc ...

"Encountered an error while trying to access properties that

Struggling to create a practice menu with the functionality of making elements appear and disappear on click? If you are encountering issues with a class named "option" not working as expected, you are not alone. Clicking on nested objects like images or i ...

Incorporate external JavaScript libraries not built with Angular

Our project utilizes NPM in conjunction with Browserify for managing third-party dependencies, which pairs well with AngularJS due to the CommonJS-modules. Below is a code snippet showcasing the dependency structure that integrates seamlessly with Angular ...

Using a loop to execute Javascript Promise.all()

I am currently facing an issue where I need to make a web API call twice inside a loop, and then wait for the results before pushing them into a larger array as subarrays. The code snippet below illustrates my approach: var latlngPairs = []; function extra ...

What could be the reason for typescript not issuing a warning regarding the return type in this specific function?

For instance, there is an onClick event handler attached to a <div> element. The handler function is supposed to return a value of type React.MouseEventHandler<HTMLDivElement> | undefined. Surprisingly, even if I return a boolean value of fal ...

Is it possible to make changes to Vue data within a function while in the process

Recently delving into the world of VueJs, I find myself navigating through laravel 6.0. While grappling with the concept of variable scopes in javascript, I sense there might be a missing piece in this puzzle. It's interesting to note that when I pla ...

You cannot edit FabricJS IText (and Textbox) within the context of React Bootstrap

I'm having trouble incorporating a canvas into my React Bootstrap component. Specifically, I am encountering issues with the text editing functionality when using IText and Textbox. Despite setting the corresponding properties, I am unable to modify t ...

Invalid extra parameters in the $.ajax function

I am attempting to access a web service in order to retrieve some data. I must include this URL using the GET method: http://localhost/ecosat/ws/api.php?t=vw_motorista However, when I check in Chrome Developer Tools, the link is shown as: http://localho ...

Execute two tasks simultaneously in two separate workers utilizing the cluster module in node.js

I'm currently diving into clustering with NodeJS. My goal is to have two separate tasks - one handling node-sass and the other managing uglifyjs - each running on a distinct worker using cluster in NodeJS. The code I've implemented seems to be fu ...

Having trouble with an endless GET request loop in NextJS 13 while utilizing the Next-Auth middleware. Experiencing difficulties fetching the RSC payload

UPDATE: The issue has been identified! It seems that the bug is caused by using the beta turbopack. I have reported this problem and we are awaiting a resolution. Query: I recently delved into a project in NextJS 13 with the new app directory setup. Afte ...