GithubHelp home page GithubHelp logo

date-timezone's Introduction

I WRITE THIS POLYFILL FOR YOU! BEST SOLUTION ONLY FOR YOU <3 MY FRIEND!1

Adding timezone control to javascript Date object. Date.setTimezoneOffset()

Using:

// Timezone to ALL dates
Date.timezoneOffset(-240) // +4 UTC

// Override timezone only for this date
new Date().timezoneOffset(-180) // +3 UTC

Coffee version:

do ->	
	# tmp date
	offsetDate = new Date()
	
	# default timezone
	Date.prototype.timezoneOffset = offsetDate.getTimezoneOffset()
	
	
	Date.setTimezoneOffset = (timezoneOffset)->
		return @prototype.timezoneOffset = timezoneOffset
	
	
	Date.getTimezoneOffset = (timezoneOffset)->
		return @prototype.timezoneOffset
	
	
	Date.prototype.getTimezoneOffset = ->
		return @timezoneOffset
	
	
	Date.prototype.setTimezoneOffset = (timezoneOffset)->
		return @timezoneOffset = timezoneOffset
	
	
	Date.prototype.toString = ->
		offsetTime = @timezoneOffset * 60 * 1000
		offsetDate.setTime(@getTime() - offsetTime)
		return offsetDate.toUTCString()
	
	
	[
		'Milliseconds', 'Seconds', 'Minutes', 'Hours',
		'Date', 'Month', 'FullYear', 'Year', 'Day'
	]
	.forEach (key)=>
		Date.prototype["get#{key}"] = ->
			offsetTime = @timezoneOffset * 60 * 1000
			offsetDate.setTime(@getTime() - offsetTime)
			return offsetDate["getUTC#{key}"]()
	
		Date.prototype["set#{key}"] = (value)->
			offsetTime = @timezoneOffset * 60 * 1000
			offsetDate.setTime(@getTime() - offsetTime)
			offsetDate["setUTC#{key}"](value)
			time = offsetDate.getTime() + offsetTime
			@setTime(time)
			return time

Compiled version:

(function() {
  var offsetDate;
  offsetDate = new Date();
  Date.prototype.timezoneOffset = offsetDate.getTimezoneOffset();
  Date.setTimezoneOffset = function(timezoneOffset) {
    return this.prototype.timezoneOffset = timezoneOffset;
  };
  Date.getTimezoneOffset = function(timezoneOffset) {
    return this.prototype.timezoneOffset;
  };
  Date.prototype.getTimezoneOffset = function() {
    return this.timezoneOffset;
  };
  Date.prototype.setTimezoneOffset = function(timezoneOffset) {
    return this.timezoneOffset = timezoneOffset;
  };
  Date.prototype.toString = function() {
    var offsetTime;
    offsetTime = this.timezoneOffset * 60 * 1000;
    offsetDate.setTime(this.getTime() - offsetTime);
    return offsetDate.toUTCString();
  };
  return ['Milliseconds', 'Seconds', 'Minutes', 'Hours', 'Date', 'Month', 'FullYear', 'Year', 'Day'].forEach((function(_this) {
    return function(key) {
      Date.prototype["get" + key] = function() {
        var offsetTime;
        offsetTime = this.timezoneOffset * 60 * 1000;
        offsetDate.setTime(this.getTime() - offsetTime);
        return offsetDate["getUTC" + key]();
      };
      return Date.prototype["set" + key] = function(value) {
        var offsetTime, time;
        offsetTime = this.timezoneOffset * 60 * 1000;
        offsetDate.setTime(this.getTime() - offsetTime);
        offsetDate["setUTC" + key](value);
        time = offsetDate.getTime() + offsetTime;
        this.setTime(time);
        return time;
      };
    };
  })(this));
})();

date-timezone's People

Contributors

umaxmaxmaximus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

rekryt cgi-cgi

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.