Skip to content

Instantly share code, notes, and snippets.

@jasp402
Created July 26, 2019 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasp402/01fc497cb2c883ce2530d29ddc2663de to your computer and use it in GitHub Desktop.
Save jasp402/01fc497cb2c883ce2530d29ddc2663de to your computer and use it in GitHub Desktop.
function pad (n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
function toFormattedDateString(timestamp, includeTime){
if(typeof includeTime == 'undefined'){
includeTime = true;
}
var date;
if(typeof timestamp === 'string'){
date = Care360.DateFormat.parseJavaTimestamp(timestamp);
}else if(typeof timestamp === 'number'){
date = new Date(timestamp);
}else{
date = '';
}
var df = includeTime ? new Care360.DateFormat('MM/dd/yyyy hh:mma') : new Care360.DateFormat('MM/dd/yyyy');
return df.format(date);
}
var Care360 = Care360 || {};
/*
Letter Date or Time Component Presentation Examples
a Am/pm marker Text PM
D Day in year Number 189
d Day in month Number 10
E Day in week Text Tuesday; Tue
h Hour in am/pm (1-12) Number 12
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
m Minute in hour Number 30
M Month in year Month July; Jul; 07
s Second in minute Number 55
S Millisecond Number 978
w Week in year Number 27
W Week in month Number 2
y Year Year 1996; 96
z Time zone Number 240 (difference in minutes from GMT)
*/
Care360.DateFormat = function(pattern) {
this.pattern = pattern;
}
Care360.DateFormat.prototype = {
format: function(date){
if (!date) return ' ';
var DAYS_OF_WEEK = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
return this.pattern.replace(/(yyyy|yy|MMMM|MMM|MM|M|w|W|D|dddd|ddd|dd|d|EE|E|hh|h|HH|H|KK|K|kk|k|mm|m|ss|s|SSS|SS|S|a|z)/g,
function($1)
{
switch ($1)
{
case 'yyyy': return date.getFullYear();
case 'yy': return date.getFullYear().toString().substr(-2);
case 'MMMM': return MONTHS[date.getMonth()];
case 'MMM': return MONTHS[date.getMonth()].substr(0, 3);
case 'MM': return pad(date.getMonth() + 1, 2);
case 'M': return date.getMonth() + 1;
case 'w': return date.getWeekInYear();
case 'W': return date.getWeekInMonth();
case 'D': return date.getDayInYear();
case 'dddd': return DAYS_OF_WEEK[date.getDay()];
case 'ddd': return DAYS_OF_WEEK[date.getDay()].substr(0, 3);
case 'dd': return pad(date.getDate(), 2);
case 'd': return date.getDate();
case 'EE': return DAYS_OF_WEEK[date.getDay()];
case 'E': return DAYS_OF_WEEK[date.getDay()].substr(0, 3);
case 'hh': return pad(((h = date.getHours() % 12) == 0 ? 12 : h), 2);
case 'h': return ((h = date.getHours() % 12) == 0 ? 12 : h);
case 'HH': return pad(date.getHours(), 2);
case 'H': return date.getHours();
case 'KK': return pad(((h = date.getHours() % 12) == 0 ? 0 : h), 2);
case 'K': return ((h = date.getHours() % 12) == 0 ? 0 : h);
case 'kk': return pad(date.getHours(), 2);
case 'k': return date.getHours();
case 'mm': return pad(date.getMinutes(), 2);
case 'm': return date.getMinutes();
case 'ss': return pad(date.getSeconds(), 2);
case 's': return date.getSeconds();
case 'SSS': return pad(date.getMilliseconds(), 3);
case 'SS': return pad(date.getMilliseconds(), 2);
case 'S': return date.getMilliseconds();
case 'a': return date.getHours() < 12 ? 'am' : 'pm';
case 'z': return date.getTimezoneOffset();
}
}
);
}
}
Care360.DateFormat.parseIso = function(strDate){
var regexIso8601 = /^(\d{4}|\+\d{6})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2}):(\d{2})\.(\d{1,3})(?:Z|([\-+])(\d{2}):(\d{2}))?)?)?)?$/;
var m = regexIso8601.exec(strDate);
if (m) {
return Date.UTC(
m[1],
(m[2] || 1) - 1,
m[3] || 1,
m[4] - (m[8] ? m[8] + m[9] : 0) || 0,
m[5] - (m[8] ? m[8] + m[10] : 0) || 0,
m[6] || 0,
((m[7] || 0) + '00').substr(0, 3)
);
}
},
Care360.DateFormat.parseJavaTimestamp = function(strDate){
if(!strDate) return '';
//java timestamp format: yyyy-MM-ddTHH:mm:ss.SSS-z
var yyyy = strDate.substr(0,4);
var MM = parseInt(strDate.substr(5,2), 0) - 1;
var dd = parseInt(strDate.substr(8,2), 0);
var HH = parseInt(strDate.substr(11,2), 0);
var mm = parseInt(strDate.substr(14,2), 0);
var ss = parseInt(strDate.substr(17,2), 0);
var SS = parseInt(strDate.substr(20,3), 0);
var date = Date.UTC(yyyy, MM, dd, HH, mm, ss, SS);
var tz = parseInt(strDate.substr(23), 0);
if(tz != 0){
date -= (tz * 36000);
}
return new Date(date);
}
jQuery.ajaxSetup({ cache: false });
function getScripts( scripts, onScript, onComplete )
{
this.async = true;
this.cache = false;
this.data = null;
this.complete = function () {
jQuery.scriptHandler.loaded();
};
this.scripts = scripts;
this.onScript = onScript;
this.onComplete = onComplete;
this.total = scripts.length;
this.progress = 0;
};
getScripts.prototype.fetch = function() {
jQuery.scriptHandler = this;
var src = this.scripts[ this.progress ];
jQuery.ajax({
crossDomain:true,
async:this.async,
cache:this.cache,
type:'GET',
url: src,
data:this.data,
statusCode: {
200: this.complete
},
dataType:'script'
});
};
getScripts.prototype.loaded = function () {
this.progress++;
if( this.progress >= this.total ) {
if(this.onComplete) this.onComplete();
} else {
this.fetch();
};
if(this.onScript) this.onScript();
};
function showOverlay() {
var topdoc = jQuery(top.document);
var overlay = jQuery('<div id="overlay"/>');
var body = topdoc.find('body');
body.append(overlay);
var framedview = jQuery("#framedview", window.parent.document.body);
framedview.data('zIndex', framedview.css('zIndex'));
framedview.css({
'background-color':'#FFFFFF',
position: 'relative',
'z-index': 101
});
}
function hideOverlay() {
var topdoc = jQuery(top.document);
topdoc.find('#overlay').remove();
var framedview = jQuery("#framedview", window.parent.document.body);
framedview.css('z-index', framedview.data('zIndex'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment