What's the right way to convert time in string format to the current Date? I'm using Moment
Expected: "15:00" -> Sat Mar 19 2022 15:00:00 "11:00" -> Sat Mar 19 2022 11:00:00
The output should be the current date and specified time. For example, if today is Mar 19 and the string time is 15:00, it should Sat Mar 19 2022 15:00:00
You can use moment's format method, to format the output according to your needs.
More about formatting options - https://momentjscom.readthedocs.io/en/latest/moment/04-displaying/01-format/
var t = "15:00";
var cdt = moment(t, 'HH:mm');
console.log(cdt.format('ddd MMM D Y HH:mm:ss'));
<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>
I think that it could be tricky just with hour as your string because you still need to provide information for the rest of the parameters that you could try when you have this string you create a variable for Date in needed style and then set hours to string you have. But if you using moment then just extend the format in which you are getting your time and you won't have to use Date