Retrieving the current date with a static time string can be achieved using Moment.js

If it is currently May 19, 2021, how can I obtain the string 2021-05-19T23:59:00.000Z?

I attempted using moment().format() but it just provides the current datetime string.

I specifically require the time in the formatString to display as 23:59, resulting in the full string being 2021-05-19T23:59:00.000Z

Any suggestions on how to accomplish this?

Answer №1

To get the end of the day using moment.js, you can use

moment().endOf("day").format()
.

console.log(moment().endOf("day").format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

If you prefer plain JavaScript, you can achieve the same result with the following code:

var end = new Date();
end.setHours(23,59,59,999);

Answer №2

If you want to obtain the end of a day using MomentJS, you can utilize the endOf method:

moment().endOf('day')

To customize the format of the output, you can combine it with the format function like so:

let currentTime = new moment();
console.log(currentTime.format());

let endOfDay = currentTime.endOf('day');
console.log(endOfDay.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Answer №3

Give this a shot:

Create a variable called "date" and set it to the current date and time in the format "YYYY-MM-DDT23:59:00.000[Z]" using the moment.js library.

Answer №4

If you need to get the end of a day, you can utilize the endOf() function provided by moment.js :

moment().endOf('day').format()

For displaying the time in your current local timezone, consider using the toString() method.

let currentTime = moment();
console.log('End of day : ' + currentTime.endOf('day').toString());

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

Storing text containing < and > symbols in a PHP Database

I am facing an issue trying to save a string from my web application into the database I'm connected to. The JavaScript successfully passes the string to PHP using AJAX, but when the insertion is performed, everything after the "<" or ">" symbo ...

Enhance your UI experience with a beautifully styled button using Material-

I was using a Material UI button with a purple background. <Button component={Link} to={link} style={{ background: '#6c74cc', borderRadius: 3, border: 0, color: 'white', heig ...

The functioning of JavaScript's ajax capabilities

Need some help with a coding issue I'm facing. Can anyone provide suggestions for improving my code? I've come across a situation where the table is not updating when using a certain piece of code. However, upon further inspection, I found that ...

Displaying child properties in a functional ReactJS component

React version 0.14 introduces the concept of pure functional components, ensuring that the same input always results in the same output. Props are passed as function arguments. // Using ES6 arrow functions with implicit return: const PureComponent = ({url ...

To begin utilizing Node.js modules, you must use the `#` symbol as the starting point

Quoting the Nodejs documentation, available at this link require(X) from module at path Y 1. If X is a core module, a. return the core module b. STOP 2. If X begins with '/' a. set Y to be the filesystem root 3. If X begins with './ ...

When I click on .toggle-menu, I want the data-target to have a toggled class and the other divs to expand

Looking to achieve a functionality where clicking on .toggle-menu will toggle a class on the specified data-target element and expand other divs accordingly. jQuery(document).ready(function() { var windowWidth = jQuery(window).width(); if (win ...

Recording videos using the Safari Browser

Within my ReactJs application, I have integrated react-multimedia-capture, a package that utilizes navigator.mediaDevices.getUserMedia and the MediaRecorder API to facilitate video recording. While I am successfully able to record videos on Chrome, Safari ...

What is the best way to display toastr messages in an Angular application?

Can you guide me on how to include toastr in an angular app? Currently, I am enrolled in the Angular Fundamentals course and trying to use toastr.success within my export class: handleThumbnailClick(eventName){ toastr.success(eventName) } But, I kee ...

Display elements that are unique to one array and not found in another array using React

I am working on a feature where users can select fruits from an array called fruits, and the selected fruits will be stored in another state array named user_fruits. Once a fruit is selected by a user, it should not be available for selection again. fruit ...

Unable to execute the new firebase on node server

I'm currently watching this google talk. However, I am facing an issue trying to create a firebase object. Following my package.json file, I initiated with: npm install firebase --save Then proceeded with the following steps: let Firebase = requir ...

Choose an input field using jQuery that is specified by its name

I am currently using jquery form validator to validate all the fields within my form. Within the form validator, I have implemented the following code snippet to determine if an input field is labeled with the name minExperience. errorPlacement: function ...

Error: Trying to access property '1' of an undefined value is not allowed

I'm experiencing a problem that I can't seem to solve. The issue arises after the user logs in. I am using useEffect() to retrieve the user data by using a secret token from localstorage. Everything seems to be working fine - the data, the secret ...

Run HTML/Angular ng-include with JavaScript

I am developing a small search app using AngularJS. I have an empty div that I want to replace with another div containing ng-include after running a function. The replacement of the div works fine, but ng-include does not load properly. For testing purpo ...

My website is experiencing issues with the functionality of the jQuery scroll feature

For a section of my website, I am utilizing jquery scroll functionality. Below is the code I am using within $(document).ready(): var div = $('#wrap'), wrapScreenHeight = div.height(), wrapHeight = div.outerHeight(), listHeight = div ...

The Vue warning indicates that there was a failed type check for the "value" prop. It was expecting an array but received a number with a value of 1

I am facing an issue with an input of type number where I need to restrict the user from entering a number greater than ten. Initially, everything was working fine until I decided to change the value to an array (from value: 1 to value: [1, 1]) After swit ...

I keep encountering an error that says "ReferenceError: localStorage is not defined" even though I have already included the "use

I have a unique app/components/organisms/Cookies.tsx modal window component that I integrate into my app/page.tsx. Despite including the 'use client' directive at the beginning of the component, I consistently encounter this error: ReferenceErr ...

Utilizing JavaScript to retrieve a JSON file from a GitHub Pages server

I'm currently working on my website hosted on GitHub pages, which only supports static websites but allows the use of JavaScript. I have a JSON file on the GitHub server that my website runs from and I need to fetch and process it using JavaScript. He ...

What are some best practices for integrating jointjs with reactjs?

Could the jointjs library be utilized alongside reactjs? Are there any libraries or wrappers that facilitate this integration? If not, what is the most straightforward method to incorporate it? ...

Issue encountered in transmitting information from JSP to servlet

I am facing an issue with receiving data from JSP to servlet. I understand that I need to serialize this data using JSON. In my JSP, I have written the following JavaScript code: var myJSONText = JSON.stringify(items); document.getElementById('test&a ...

Is it possible for Node.js to execute individual database operations within a single function atomically?

As I delve into writing database queries that operate on node js, a puzzling thought has been lingering in my mind. There seems to be a misunderstanding causing confusion. If node is operating in a single-threaded capacity, then it follows that all functi ...