]> code.citadel.org Git - citadel.git/blobdiff - webcit-ng/static/js/util.js
Message date/time is now sent to the browser in unix timestamp format
[citadel.git] / webcit-ng / static / js / util.js
index 5e80a898644d22976b9dab0e25c84aa9598a25ab..201247450aa720bd6803710bcfb424ce42046597 100644 (file)
@@ -82,3 +82,35 @@ function escapeJS(text) {
                return '\\' + a ;
        });
 }
+
+
+// Convert a UNIX timestamp to the browser's local time
+// Shamelessly swiped from https://gist.github.com/kmaida/6045266
+function convertTimestamp(timestamp) {
+       var d = new Date(timestamp * 1000),                     // Convert the passed timestamp to milliseconds
+               yyyy = d.getFullYear(),
+               mm = ('0' + (d.getMonth() + 1)).slice(-2),      // Months are zero based. Add leading 0.
+               dd = ('0' + d.getDate()).slice(-2),             // Add leading 0.
+               hh = d.getHours(),
+               h = hh,
+               min = ('0' + d.getMinutes()).slice(-2),         // Add leading 0.
+               ampm = 'AM',
+               time;
+                       
+       if (hh > 12) {
+               h = hh - 12;
+               ampm = 'PM';
+       }
+       else if (hh === 12) {
+               h = 12;
+               ampm = 'PM';
+       }
+       else if (hh == 0) {
+               h = 12;
+       }
+       
+       // ie: 2013-02-18, 8:35 AM      
+       time = yyyy + '-' + mm + '-' + dd + ', ' + h + ':' + min + ' ' + ampm;
+               
+       return time;
+}