-2

I need to convert a string time (ie: 1100) to a DateTime using the current day/month/year and the given time.

This is what I'm currently doing, and isn't working properly...

data.BeginTime = 1100
beginTime = new Date(data.BeginTime)

EDIT:

I figured it out with the following:

var d = new Date();
var bT = new Date(d.getFullYear(), d.getMonth(), d.getDay(), data.BeginTime / 100, 0, 0, 0);
RJ Reedy
  • 52
  • 9

1 Answers1

0

You could do this with a function like this:

function todayAtTime(militaryTime) {
    var minutes = militaryTime % 100;
    var hours = Math.floor((militaryTime - minutes) / 100);
    var dt = new Date();
    dt.setUTCHours(hours);
    dt.setUTCMinutes(minutes);
    dt.setUTCSeconds(0);
    dt.setUTCMilliseconds(0);
    return dt;
}

// Example call
var result = todayAtTime(1100); 

// Output result
console.log(result);

This is UTC Date Time. If you are going to use the date as a locale date time, then leave out the "UTC" part in the method calls, and print as result.toLocaleString().

trincot
  • 317,000
  • 35
  • 244
  • 286