0

Say that I have DateTime in this format Fri Feb 02 2018 00:00:00 GMT+0530 (IST)

And from the time picker plugin getting the time 1:10am or 2:30pm in this format.

I am not sure how to calculate and combine/add them both to produce this result:
Fri Feb 02 2018 01:10:00 GMT+0530 (IST) or Fri Feb 02 2018 14:30:00 GMT+0530 (IST)

I wish if there was something to do as simple as this:
new Date(dateString).setHours(1:10am)

Syed
  • 15,657
  • 13
  • 120
  • 154

3 Answers3

4

Seems like you need to parse it on your own:

function parseDaytime(time) {
  let [hours, minutes] = time.substr(0, time.length  -2).split(":").map(Number);
  if (time.includes("pm") && hours !== 12) hours += 12;
  return 1000/*ms*/ * 60/*s*/ * (hours * 60 + minutes);
}

To add it to a date:

new Date(
  +new Date("Fri Feb 02 2018 00:00:00 GMT+0530")
  +parseDaytime("1:20pm")
);
jo_va
  • 13,504
  • 3
  • 23
  • 47
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
3

Here is a simple function to do what your after.

It basically splits the time using a regex, and then calls setHours & setMins, adding 12 hours if pm is selected.

The example below takes the current datetime, and sets 1:10am & 2:40pm..

function setHours(dt, h) {
  var s = /(\d+):(\d+)(.+)/.exec(h);
  dt.setHours(s[3] === "pm" ? 
    12 + parseInt(s[1], 10) : 
    parseInt(s[1], 10));
  dt.setMinutes(parseInt(s[2],10));
}

var d = new Date();
console.log(d);
setHours(d, "1:10am");
console.log(d);
setHours(d, "2:40pm");
console.log(d);
Keith
  • 22,005
  • 2
  • 27
  • 44
  • Well, 12AM is 00. This does not work for that case. – Canser Yanbakan Aug 01 '23 at 14:18
  • @CanserYanbakan AM / PM is one of those locale oddities, I'm in the UK and it's `0:00 am` & `0:00 pm` , eg. -> `console.log(new Date('2020-01-01').toLocaleString('en-GB', { hour: 'numeric', minute: 'numeric', hour12: true }));` = `0:00 am` But yes, if your from another country then you might want to alter accordingly. Or to be fair there might be some better alternative now, as this was written over 5 years ago now.. – Keith Aug 01 '23 at 14:33
1

You can parse the time string into hours & minutes, adjust the hours according to am/pm & set it to the date object then:

var dateString = 'Fri Feb 02 2018 00:00:00 GMT+0530 (IST)';
var hoursString = '2:30pm';
var parts = hoursString.replace(/am|pm/, '').split(':')
var hours = parseInt(parts[0]) + (hoursString.indexOf('pm') !== -1 ? 12 : 0);
var minutes = parts[1];
var date = new Date(dateString);
date.setUTCHours(hours, minutes);
console.log(date); // in your local time
console.log(date.toUTCString()); // in UTC (i.e. without timezone offset)

(Note setHours / setUTCHours mutates date object but returns unix timestamp of the updated datetime.)

Tomas Varga
  • 1,432
  • 15
  • 23
  • thanks for your help, the output was suppose to be `Fri Feb 02 2018 14:30:00 GMT+0530 (IST)` but getting `Thu Feb 01 2018 20:00:00 GMT+0530 (IST)` – Syed Feb 02 '18 at 12:38
  • You are getting the result with your timezone offset (14:30 + 05:30 = 20:00), getting UTC value should be done with `date.getUTCString()` resp. `utcDate = new Date(date.getUTCString());` (edited the answer) – Tomas Varga Feb 02 '18 at 13:36