0

convert number to data type time


let time = 19:30; /// this is a number

let time2 = 19:30; /// this is a time 

I want to convert numbers to hours and minutes (time)

SavceBoyz
  • 59
  • 5

1 Answers1

1

function toGetTime() {
  let time = "19:30"; //input time as String
  time = time.split(':');
  let now = new Date();
  var date = new Date(now.getFullYear(), now.getMonth(), now.getDate(), ...time);
  var toTime = date.toLocaleTimeString('it-IT')
  console.log(toTime)
}

toGetTime();

You may input time as a string not actual numbers. In the code above, it will extract time from the Date and time format of Javascript.

There really is no type time we only have String, Number, Booleans, undefined and null. So if you really want it in a time format it will be a date-time format in the form of String. From here you can extract the time.

You can also reference these links:

Twilight
  • 1,399
  • 2
  • 11
  • So you parse a string to a Date then format the date as a string virtually identical to the original string. You could just return `time + ':00'`. – RobG Oct 04 '22 at 05:48