简体   繁体   English

编辑日期对象Javascript

[英]Edit Date object Javascript

I'm trying to edit the javascript date object's way of retrieving a date from the system clock. 我正在尝试编辑javascript日期对象从系统时钟中检索日期的方式。 I am testing a Javascript application and this isn't a change that would be for production, strictly for testing purposes. 我正在测试Javascript应用程序,但这并不是用于生产的更改,严格用于测试目的。

I would like to pass a parameter with year, month, and day and let the date object retrieve that rather than getting the date from the system clock. 我想传递带有年,月和日的参数,并让date对象检索该参数,而不是从系统时钟获取日期。

The problem with creating a new instance is it will return the same date without updating the time with the system clock. 创建新实例的问题是它将返回相同的日期而不用系统时钟更新时间。 Ie returning 即返回

Date {Fri Jan 20 2012 00:00:00 GMT+0700 (SE Asia Standard Time)}
Date {Fri Jan 20 2012 00:00:00 GMT+0700 (SE Asia Standard Time)}
Date {Fri Jan 20 2012 00:00:00 GMT+0700 (SE Asia Standard Time)}
Date {Fri Jan 20 2012 00:00:00 GMT+0700 (SE Asia Standard Time)}

By editing the year, month and day only, I need to keep the minutes, second, etc. to update with the system clock and not be static for testing purposes. 通过仅编辑年,月和日,我需要保留分钟,秒等以更新系统时钟,并且出于测试目的不是静态的。

This is what I'm doing do far, but has not worked for me. 这是我目前正在做的事情,但对我没有用。

    Date = function (Date) {
        return function () {
            var date = new Date();
            date.setFullYear(%s)
            date.setMonth(%s);
            date.setDate(%s);
            return date;
        }
    }(Date)
    alert(new Date())

EDIT : The main problem with this that I'm having is I need to open the console in the window that I'm testing in and have this appear to be the date that I want while if it does not keep the seconds constant http://postimg.org/image/6hzjw4h7j/ 编辑 :我遇到的主要问题是我需要在正在测试的窗口中打开控制台,并且如果它不能保持秒数不变,那么这似乎是我想要的日期http: //postimg.org/image/6hzjw4h7j/

EDIT : The solution that has worked to change the browsers time is as follows 编辑 :已更改浏览器时间的解决方案如下

        Date = function(Date){
            return function() {
                    date = new Date();
                    date.setTime(date.getTime() - %d);
                    return date;
            }
        }(Date);

Thanks to everyone for their input and hope this helps others. 感谢大家的投入,希望对大家有所帮助。

I am not sure whether this is what you want. 我不确定这是否是您想要的。

function MyDate(year,month,day) {
  var date = new Date();
  date.setFullYear(year);
  date.setMonth(month);
  date.setDate(day);
  return date;
}  

var myDate = new MyDate(1990,04,10);

console.log(myDate);

If you have a <div> like this: 如果您有这样的<div>

<div id="mydate"></div>

You could update it continuously like this: 您可以像这样不断更新它:

var mydate = new Date('01/01/2015 00:00:55');
var el = document.getElementById('mydate');
setInterval( function(){
  mydate.setSeconds(mydate.getSeconds() + 1);
  el.innerText=mydate.toLocaleString();
} , 1000);

Here is a fiddle . 这是一个小提琴

I'm not going to touch on replacing the date object constructor itself (if that's what you meant), but I think this is pretty much what you are looking to do: 我不打算替换日期对象构造函数本身(如果那是您的意思),但是我认为这几乎是您要执行的操作:

[EDITED - replaced date object constructor]: [编辑-替换日期对象构造函数]:

Date = (function(Date) {
    return function(y,m,d,h,min,s,mil) {
        var newDateObj;
        if(mil !== undefined)
            newDateObj = new Date(y,m,d,h,min,s,mil);
        else if(s !== undefined)
            newDateObj = new Date(y,m,d,h,min,s);
        else if(min !== undefined)
            newDateObj = new Date(y,m,d,h,min);
        else if(h !== undefined)
            newDateObj = new Date(y,m,d,h);
        else if(d !== undefined)
            newDateObj = new Date(y,m,d);
        else if(m !== undefined)
            newDateObj = new Date(y,m);
        else if(y !== undefined)
            newDateObj = new Date(y);
        else newDateObj = new Date();
        // Set up date object, offsets, and update function
        var dateOffset = new Date().getTime()-newDateObj.getTime();
        var dateObject = new Date(new Date().getTime()-dateOffset);
        var updateDateObj = function() {
            dateObject.setTime(new Date().getTime()-dateOffset);
            };
        // Either create a wrapper for all date/time functions to update:
        var doGetTime = dateObject.getTime;
        dateObject.getTime = function() {
            updateDateObj();
            return doGetTime.call(dateObject);
            };
        // Or do an update on interval (not reccommended):
        window.setInterval(updateDateObj, 1);
        return dateObject;
        };
    }(Date));
var x = new Date(2014, 10, 20);
window.setInterval(function() {
    document.body.innerHTML = x;
    }, 1000);

You can see the fiddle here . 您可以在这里看到小提琴 You can see the edited fiddle here . 您可以在这里看到编辑过的小提琴

After some thinking, I've decided to add another answer. 经过一番思考,我决定添加另一个答案。 After re-checking your posts and comments I have come up with this and I'm sure you'll accept this as the correct answer: 重新检查您的帖子和评论后,我提出了这个建议,我相信您会接受此作为正确答案:

[UPDATE: Added newJSSystemDate layer] [更新:添加了newJSSystemDate层]

// set up newJSSystemDate with complicated scoping magic
var newJSSystemDate = (function(actualDateObject){
    return function(newDate) {
        // use actual date if new date is not specified
        if(newDate === undefined) Date = actualDateObject;
        // otherwise update global constructor for Date()
        else Date = (function(Date) { // calculate offset
            var dtOffset = new Date().getTime()-newDate.getTime();
            return function(y,m,d,h,min,s,mil) {
                var newDateObj;
                // handle all correct usages of Date()
                if(mil !== undefined)
                    newDateObj = new Date(y, m, d, h, min, s, mil);
                else if(s !== undefined)
                    newDateObj = new Date(y, m, d, h, min, s);
                else if(min !== undefined)
                    newDateObj = new Date(y, m, d, h, min);
                else if(h !== undefined)
                    newDateObj = new Date(y, m, d, h);
                else if(d !== undefined)
                    newDateObj = new Date(y, m, d);
                else if(m !== undefined)
                    newDateObj = new Date(y, m);
                else if(y !== undefined)
                    newDateObj = new Date(y);
                else // return calculated date object
                    newDateObj = new Date(new Date().getTime()-dtOffset);
                return newDateObj;
                };
            }(actualDateObject));
        };
    }(Date));
// set js time to a specific date/time
newJSSystemDate(new Date(2014, 10, 20));
// check what Date() returns every second
window.setInterval(function() {
    document.body.innerHTML = new Date();
    }, 1000);
// set js time back to normal after 10 seconds
window.setTimeout(function() {
    newJSSystemDate();
    }, 10000);

Click here for the JSFiddle 单击此处获取JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM