Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
JS: convert time to a Date format

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

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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>

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!