Skip to content

Instantly share code, notes, and snippets.

@iSkore
Created October 19, 2016 12:00
Show Gist options
  • Save iSkore/c98a96cc664a4856739d647b0196de9e to your computer and use it in GitHub Desktop.
Save iSkore/c98a96cc664a4856739d647b0196de9e to your computer and use it in GitHub Desktop.
Time stamps, time zone offset, clean converter
'use strict';
class Time
{
constructor( tz )
{
this.tz = tz;
this.tzOffset = 3600000 * this.tz || 0;
this.days = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ];
this.months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ];
this.now = () => Date.now() + this.tzOffset;
}
getNow()
{
this.time = this.now();
return this.time;
}
getTimeStamp( t )
{
return new Date( t );
}
zeroOffset( n )
{
return ( n < 10 ? '0' : '' ) + n;
}
getDate( t )
{
t = this.getTimeStamp( t );
const
mo = t.getMonth() + 1,
monthName = this.months[ mo - 1 ],
d = t.getDate(),
h = t.getHours(),
m = t.getMinutes(),
s = t.getSeconds(),
ms = t.getMilliseconds();
return {
year: t.getFullYear(),
month: this.zeroOffset( mo ),
monthName,
day: this.zeroOffset( d ),
dayName: this.days[ t.getDay() ],
hour: this.zeroOffset( h ),
minute: this.zeroOffset( m ),
second: this.zeroOffset( s ),
ms
};
}
timeSince1970( t )
{ // Not a correct equation
return ( t / 1000 ) / 60 / 60 / 24 / 7 / 52;
}
}
let t = new Time( 0 );
console.log( t );
console.log( t.getTimeStamp( t.now() ) );
console.log( t.getDate( t.now() ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment