Skip to content

Instantly share code, notes, and snippets.

@hoosin
Created November 11, 2014 02:39
Show Gist options
  • Save hoosin/c7072c06cabf5340d382 to your computer and use it in GitHub Desktop.
Save hoosin/c7072c06cabf5340d382 to your computer and use it in GitHub Desktop.
getNow 模拟本地时间
var getNow = (function () {
//DEBUG 为false则返回真实时间,ture 则返回模拟时间。
var DEBUG = true;
//通过修改年、月、日 时 分 秒 毫秒 来达到某个时间的模拟。
var now = new Date(2014, 10, 24, 0, 0, 0, 0).getTime();
var begin = new Date();
return function () {
if (DEBUG) {
var t = new Date().getTime();
now += t - begin;
begin = t;
return new Date(now);
}
return new Date();
};
})();
//这样代码中的new Date()替换为getNow(),在需要模拟修改时间的时候,设置DEBUG为true,然后指定模拟的时间。切记,调试完毕将DEBUG设置为false。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment