retrieve today's date with parsed time using moment

I am attempting to retrieve the current date and time, with the specified time using moment js. Here's what I have tried.


const time = '18:00'
const timeAndDate = moment(time)

However, when I display timeAndDate, it indicates an invalid date. Is there a different method to accomplish this?

Answer №2

To specify the time format, simply include it as the second parameter in moment's function call.

const timeOfToday = moment('18:00', 'HH:mm');
console.log(timeOfToday);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

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

Tips for compressing user data in JavaScript prior to transmitting it to the server using zip/gzip technology

As a Javascript novice, I am facing a challenge with multiple users sending large JSON payloads to the server. To reduce traffic, I want to compress them using gzip. Can gzip compression be implemented in Javascript? How can I convert the JSON string int ...

javascript: update hidden field if date is past January 15th

Having a bit of trouble with this one after a full day! The form contains a hidden field called 'grant_cycle'. If the form is submitted after January 15th, it should have the value 'Spring, [year]', or if after July 15th, it should be ...

Sending the slider value from a website to a program when the slider is adjusted

I have been experimenting with programming an ESP32 to regulate the LED brightness using a slider. I've pieced together some information from tutorials found on and Currently, I've achieved the ESP32 connecting to my network, displaying the sli ...

The glitch in VueJS's array updating functionality

I am facing an issue where I have an array called tiles with a title. On creating the instance, I add a field to this array named active. Subsequently, I display this array within an <li> element and iterate through it to display the title and activ ...

How can you use the MongoDB Aggregation Framework to filter by a range of dates, group results by individual days, and calculate the average value for each day

I'm currently exploring the possibilities of MongoDB's Aggregation Framework and would appreciate some assistance in enhancing this query to achieve the following objectives: Retrieve Records with Dates falling within a specified range Organize ...

Issue with jQuery .on('change') function not functioning correctly following a row being duplicated

Within my input form, I have implemented an on-click function that triggers an ajax call to submit a table row to the database. Upon submission, the row is then duplicated so that the user can input more data if necessary. The issue I am currently facing r ...

Steady Navigation Bar in Javascript without Bouncing

While experimenting with a fixed navigation bar, I've encountered an issue where the content below jumps up on the page when the navigation bar "fixes" itself to the top of the screen. You can check out the JSFiddle I've been working on for refer ...

Display information from a mysql database table within a selection menu

Currently, I am working on a dropdown menu that should display data from a MySQL table. However, I am facing an issue which is outlined below: In my PHP script, I have approached it in the following manner: <label class="col-form-label" for="formGrou ...

Accessing files from a Vue project in VS Code and making them available in Chrome

I am new to web development, so I appreciate your patience with me. I am currently working with threejs and looking to load an svg using the SVGLoader example. However, my main concern is not about the loading function, but rather about making the svg file ...

Concerns have been raised regarding the lack of light and shadows being detected by THREE.BufferGeometry in JavaScript/th

I've been trying to generate terrain using noise functions, but I'm running into an issue where the BufferGeometry mesh isn't receiving any light from PointLight. When I switch to AmbientLight, the BufferGeometry is visible, but not with Poi ...

Unexpected error (in promise): Unable to access the property '$store' because it is undefined

I am working on developing an application using vue3 and vuex. I encountered an error when trying to use $store (Uncaught (in promise) TypeError: Cannot read property '$store' of undefined). Despite searching, I have not been able to find a solut ...

Transform a group of objects in Typescript into a new object with a modified structure

Struggling to figure out how to modify the return value of reduce without resorting to clunky type assertions. Take this snippet for example: const list: Array<Record<string, string | number>> = [ { resourceName: "a", usage: ...

Unable to place a breakpoint in a .vue file containing an asynchronous method

I am encountering some challenges while trying to debug my Vue application. It seems that when I step through the code, it either skips lines or I cannot set breakpoints on valid lines. I suspect this might be an issue with the sourcemap, but I am not cer ...

Why is the Jquery console not displaying any values?

Hey everyone, I need some help with a small issue in my project. For some reason, the console.log() function in my project is not returning any values. <script> $('#search-box<?=$x;?>').blur(function() { var val = $("#search ...

What causes the console.log function to behave in this manner?

When using the node.js interpreter, if you run the code: console.log("A newline character is written like \"\\ n \"."); //output will be:- // A newline character is written like "\ n ". However, if you just enter the following in ...

Employing jQuery to add an element as a sibling rather than a child node

I'm having trouble finding the specific functionality I need. My goal is to add sibling DOM elements to a disconnected node. From what I gather, it should be possible with either .after() or .add(), but for some reason both methods are not working as ...

Babel Compile disrupts the flow of commands

I'm facing an issue while attempting to launch my development server after Babel successfully compiles my files. However, the command chain seems to halt right after Babel displays the compilation success message. Babel has completed compiling 82 f ...

"Bringing in a Nunjucks Macro - How to Easily Use It

I've been working on a script that renders a nunjucks contact.html template with the following code: let fs = require('fs'); let nj = require('nunjucks'); var contact = fs.readFileSync('./src/contact.html',&a ...

The utilization of ES Modules within a Next.js server.js

After reviewing a few examples in the Next.js repository, I came across: https://github.com/zeit/next.js/tree/master/examples/custom-server-express https://github.com/zeit/next.js/tree/master/examples/custom-server-koa I observed that these examples ut ...

Tips on personalizing the vue-filemanager interface within Laravel

I'm currently utilizing the Laravel file manager package from here, which provides a pre-compiled JS file or allows for direct use of the vue-component through npm from here. Unfortunately, in both options, the front-end is not customizable. I have i ...