Please help! I am a very novice JavaScript user and have scoured the forums trying to find an answer to my problem:
I have two fields that are variable I am trying to add them together in a separate field.
One field (TotHr) is in HH:MM format, and the other is a datetime field (DTin1) that is in m/d/yy h:MM tt format.
I need the output to read in the same format as the datetime field.
This will be utilized on various consoles throughout my work, so I would like to not use a library like moment.js.
Here is what I have so far:
var date = new Date();
var dateMil = date.getTime("DTin1");
var timePeriod = ("TotHr");
var parts = timePeriod.split(/:/);
var timePeriodMil = (parseInt(parts[0], 10) * 60 * 60 * 1000) +
(parseInt(parts[1], 10) * 60 * 1000) +
(parseInt(parts[2], 10) * 1000);
var newDate = new Date();
newDate.setTime(dateMil + timePeriodMil);
console.log(newDate);
There are many ways to do it in JavaScript. A simple way is to use some library e.g. the following code uses moment.js library.
const { duration } = require('moment')
const moment = require('moment-timezone')
let strDuration = "10:30"
let strDateTime = "9/6/21 5:10 am"
let format = 'M/D/YY h:m a'
dur = moment.duration(strDuration)
dt = new moment(strDateTime, format)
result = dt.add(dur).tz('Europe/London').format(format)
console.log(result)
Output:
9/6/21 3:40 pm