I managed to convert the time format from GMT to my browser's local time using the following JavaScript code:
var newDate = new Date(timeFromat);
timeFormat = newDate.toLocaleString();
Now, I want to change the time format to a 24-hour clock format like this: 12/16/2011 15:49:37 and I need to accomplish this in JavaScript.
This is what I have tried so far:
var firstPartOftimeFormat = timeFormat.substring(0,9);
var secondPartOftimeFormat = timeFormat.substring(10,20);
However, this method doesn't work properly when the date format is different, such as: 3/16/2011. The following part of the code seems to be effective though.
var time = $("#starttime").val();
var hours = Number(secondPartOftimeFormat.match(/^(\d+)/)[1]);
var minutes = Number(secondPartOftimeFormat.match(/:(\d+)/)[1]);
var AMPM = secondPartOftimeFormat.match(/\s(.*)$/)[1];
if(AMPM == "PM" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10) sHours = "0" + sHours;
if(minutes<10) sMinutes = "0" + sMinutes;
alert(sHours + ":" + sMinutes);
Do you have any alternative suggestions or approaches to achieve this? Your help would be greatly appreciated. Thank you.