Modern JavaScript (Node.js)
function ConvertTime_Node(dateStr)
{
let dateObj = new Date(dateStr);
// Convert 5 hours 30 minutes to milliseconds
const hoursToAdd = 5;
const minutesToAdd = 30;
var millisecondsToAdd = hoursToAdd * 60 * 60 * 1000 + minutesToAdd * 60 * 1000;
// Change time
dateObj.setTime(dateObj.getTime() + millisecondsToAdd);
// Format
let options =
{
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: true
};
const formatter = new Intl.DateTimeFormat("en-US", options);
const result = formatter.format(dateObj);
return result;
}
Microsoft JScript Version (WScript)
function ConvertTime_WScript(dateStr)
{
var dateObj = new Date(dateStr);
// Convert 5 hours 30 minutes to milliseconds
var hoursToAdd = 5;
var minutesToAdd = 30;
var millisecondsToAdd = hoursToAdd * 60 * 60 * 1000 + minutesToAdd * 60 * 1000;
// Change time
dateObj.setTime(dateObj.getTime() + millisecondsToAdd);
// Format
var month = dateObj.getMonth() + 1; // Months are 0-based in JavaScript
var day = dateObj.getDate();
var year = dateObj.getFullYear();
var hours = dateObj.getHours();
var minutes = dateObj.getMinutes();
var seconds = dateObj.getSeconds();
// Format the hours to be in 12-hour format and get the AM/PM part
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
// Pad the minutes and seconds with a zero if they are less than 10
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
// Format the date string
var result = month + '/' + day + '/' + year + ' ' + hours + ':' + minutes + ':' + seconds + ' ' + ampm;
return result;
}