I'm trying to convert a Date object into a weird format, like so:
03/30/2017 11:37:14:082 -0500
The issue that really trips me up is the timezone offset. There's no way that screams at me as easy to do, so here's my implementation (in coffeescript, hopefully it's easy enough to understand without intimate knowledge of coffeescript):
timeStamp = new Date() offsetHours = timeStamp.getTimezoneOffset() / 60 * 100 if offsetHours <= -1000 offsetString = "+" + Math.abs(offsetHours) else if offsetHours == 0 offsetString = "+0000" else if offsetHours < 0 and offsetHours > -1000 offsetString = "+0" + Math.abs(offsetHours) else if offsetHours >= 1000 offsetString = "-" + offsetHours else offsetString = "-0" + offsetHours timeStampString = (timeStamp.getMonth() + 1) + '/' + timeStamp.getDay() + '/' + timeStamp.getFullYear() + " " + timeStamp.getHours() + ":" + timeStamp.getMinutes() + ":" + timeStamp.getSeconds() + ":" + timeStamp.getMilliseconds() + " " + offsetString
I realize my implementation isn't handling time-zones that have 30 minute offsets (which will get fixed) but I am looking to see if anyone can poke any other holes they see here. This code kind of smells to me and I feel like there should be an easier way to accomplish this.
I've spoofed the date prototype for JS to test a variety of time-zones and haven't found anything weird yet. This is to support interop with another application so "use a different scheme" unfortunately isn't an option. I'd like to avoid using libraries as well.