I have written some Javascript to convert a database datetime in UTC format into local datetime. It works fine in Firefox and IE, but does not work in Chrome (for some reason it adds 3 hours to the times). Here is my code, any advice would be very appreciated :)
EDIT : The datetime is passed into my first function in the format '2014-06-21 20:00:00'
function convertUTCTimeToLocal(utc24HrDateTime) {
//first convert from 24hr to 12hr clock
var twentyFourHrTime = convertDateToTime(utc24HrDateTime);
var twelveHrTime = convert24hrTo12Hr(twentyFourHrTime);
var twelveHrDateTime = convert12HrToDateTime(utc24HrDateTime, twelveHrTime);
var utc12HrDateTime = twelveHrDateTime + ' UTC';
var date = new Date(utc12HrDateTime);
var dayMonthTimeOnly = convertToDayMonthTimeOnly(date);
return dayMonthTimeOnly;
}
function convert24hrTo12Hr(time) {
// Check correct time format and split into components
time = time.toString().match(/^([01]\d|2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/) || [time];
if (time.length > 1) { // If time format correct
time = time.slice(1); // Remove full string match value
time[5] = +time[0] < 12 ? ' AM' : ' PM'; // Set AM/PM
time[0] = +time[0] % 12 || 12; // Adjust hours
}
return time.join(''); // return adjusted time or original string
}
function convertDateToTime(fixtureDate) {
var d = new Date(fixtureDate); // now
var y = d.getFullYear(); // the full year (4 digits)
var m = d.getMonth() + 1; // 0-based month
var dt = d.getDate(); // 0-based day of the month
dt = dt < 10 ? '0' + dt : dt; // add a preceding 0 to numbers less than 10
var h = d.getHours(); // 0-based hours
h = h < 10 ? '0' + h : h; // add a preceding 0 to numbers less than 10
var mn = d.getMinutes(); // minutes
mn = mn < 10 ? '0' + mn : mn; // add a preceding 0 to numbers less than 10
return h + ':' + mn;
}
function convert12HrToDateTime(fixtureDate, twelveHrTime) {
var d = new Date(fixtureDate); // now
var y = d.getFullYear(); // the full year (4 digits)
var m = d.getMonth() + 1; // 0-based month
var dt = d.getDate(); // 0-based day of the month
dt = dt < 10 ? '0' + dt : dt; // add a preceding 0 to numbers less than 10
return m + '/' + dt + '/' + y + ' ' + twelveHrTime;
}
function convertToDayMonthTimeOnly(fixtureDate) {
var d = new Date(fixtureDate); // now
var y = d.getFullYear(); // the full year (4 digits)
var m = d.getMonth() + 1; // 0-based month
var dt = d.getDate(); // 0-based day of the month
dt = dt < 10 ? '0' + dt : dt; // add a preceding 0 to numbers less than 10
var h = d.getHours(); // 0-based hours
h = h < 10 ? '0' + h : h; // add a preceding 0 to numbers less than 10
var mn = d.getMinutes(); // minutes
mn = mn < 10 ? '0' + mn : mn; // add a preceding 0 to numbers less than 10
return dt + '/' + m + ' ' + h + ':' + mn;
}