简体   繁体   中英

Edit Date object Javascript

I'm trying to edit the javascript date object's way of retrieving a date from the system clock. I am testing a Javascript application and this isn't a change that would be for production, strictly for testing purposes.

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.

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/

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 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]

// 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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