
This sounded straightforward at first, I had to display the user’s date and time on an online web report. And from searching online, seems it can only be done in Javascript. Here is the code:
var months = new MonthArray(‘Jan’,‘Feb’,‘Mar’,‘Apr’,‘May’,‘Jun’,‘Jul’,‘Aug’,‘Sep’,‘Oct’,‘Nov’,‘Dec’);
var vUsersDate = new Date();
var day = vUsersDate.getDate();
var mon = vUsersDate.getMonth() + 1;
var year = vUsersDate.getYear();
var hour = vUsersDate.getHours();
var min = vUsersDate.getMinutes();
if (hour < 12) { a_p = “AM”; } else{ a_p = “PM”;}
if (hour == 0) { hour = 12;}
if (hour > 12) { hour = hour – 12;}
//quickfix: min.length is undefined otherwise
min = “” + min;
if (min.length < 2) { min = “0″ + min; }
//dd-MMM-yyyy hh:mm tt
document.getElementById(‘cDate’).innerText = day + “-” + months[mon] + “-” + year + ” “ + hour + “:” + min + ” “ + a_p
This works great, however, since minute will only have 1 digit if its <10, I had to add a 0 in front of it. I thought it was a simple case or checking the length and adding a ‘0′, but I have no clue why I get an error saying that min.length is undefined. Perhaps, someone could enlighten me on this in the comments, and that would be great, cause well quickfixes are basically, well, quickfixes




