Last active
August 30, 2019 14:45
-
-
Save hitGovernor/58fbb2564921318258c4cafa154f7849 to your computer and use it in GitHub Desktop.
timeParting.js - Returns {day|julian|yyyy-mm-dd|hh:00|offset|timezone}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var timeParting = { | |
/** | |
* @description returns utc offset based on local time (eg//utc-05 = central daylight time, north america) | |
* a list of utc offsets and timezones can be found here: http://forbrains.co.uk/international_tools/earth_timezones | |
* @param pv_date {date} | |
*/ | |
getTimezoneOffset: function (pv_date) { | |
var offset = ((pv_date.getTimezoneOffset() / 60) * -1); //multiply by -1 to account for inverted utc offset in JS getTimezoneOffset() | |
var posOrNeg = (offset > 0) ? "+" : "-"; | |
var tmpOffset = offset + ""; | |
tmpOffset = tmpOffset.split("."); | |
var offsetLeft = Math.abs(tmpOffset[0]) + ""; | |
var offsetRight = ((tmpOffset[1] === "25") ? "15" : ((tmpOffset[1] === "5") ? "30" : ((tmpOffset[1] === "75") ? "45" : "00"))); | |
//build offset string | |
offset = posOrNeg + | |
((offsetLeft < 10) ? "0" + offsetLeft : offsetLeft) //include leading 0, if necessary | |
+ | |
((offsetRight === "00") ? "" : ":" + offsetRight); //include partial hour info, if necessary | |
return ("UTC" + offset); | |
}, | |
/** | |
* @description returns time zone | |
* @example America/Chicago | |
*/ | |
getTimezone: function () { | |
return Intl.DateTimeFormat().resolvedOptions().timeZone; | |
}, | |
/** | |
* @param date {date} | |
* @param granularity {=string} 'hour', defaults to minute ommitted or specified with any other value | |
*/ | |
getTime: function(date, granularity) { | |
if(granularity === 'hour') { | |
return ("0" + date.getHours()).slice(-2) + ":00"; | |
} else { | |
return ("0" + date.getHours()).slice(-2) + ":" + ((date.getMinutes() < 10) ? "0" + date.getMinutes() : date.getMinutes()); | |
} | |
}, | |
/** | |
* @description calculates current local date based on browser/client machine; returns date as string | |
*/ | |
getLocalTime: function () { | |
var dayName = { | |
0: "Sunday", | |
1: "Monday", | |
2: "Tuesday", | |
3: "Wednesday", | |
4: "Thursday", | |
5: "Friday", | |
6: "Saturday" | |
} | |
var date = new Date(); | |
var localDate = { | |
time: this.getTime(date, 'hour'), | |
weekday: dayName[date.getDay()], | |
julian: Math.ceil((date - new Date(date.getFullYear(),0,1)) / 86400000), | |
month: ("0" + (date.getMonth() + 1)).slice(-2), | |
day: ("0" + date.getDate()).slice(-2), | |
year: date.getFullYear(), | |
offset: this.getTimezoneOffset(date), | |
timezone: this.getTimezone() | |
} | |
var retVal = | |
localDate.weekday + "|" + | |
localDate.julian + "|" + | |
localDate.year + "-" + localDate.month + "-" + localDate.day + "|" + | |
localDate.time + "|" + | |
localDate.offset + "|" + | |
localDate.timezone; | |
return (retVal); | |
} | |
} | |
// timeParting.getLocalTime(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment