0

I want to override javascript Date. Aim is to get time 1 hour prior to the current time. But if arguments are passed to the Date function, it should behave in a default way. Is there any way to achieve this ? (should work on IE11 as well)

I have tried something like this but it fails for certain arguments.

Date = function(options) {

Date.prototype = x.prototype;
if(options)
    return new x(Array.prototype.slice.call(arguments).join())
else
    return new x(x.now()-(1000*60*60))

}

Tanay Narkhede
  • 355
  • 3
  • 19
  • Could you explain why you need this? – t.niese Mar 06 '22 at 16:17
  • @t.niese in some cases I have custom delay setup in my product. Currently we have used new Date() everywhere. So I need to override it completely so that code in the product is not affected. – Tanay Narkhede Mar 06 '22 at 16:20
  • 3
    That is a perfect way to make your code base completely unmanageable. Don't do this. – trincot Mar 06 '22 at 16:21
  • @trincot can you suggest some way to solve my use case without overriding it ? – Tanay Narkhede Mar 06 '22 at 16:23
  • 1
    For that you have to really explain and convince us of the deeper problem you try to solve. – trincot Mar 06 '22 at 16:24
  • 2
    You should fix the code in your code base. Messing around with the default behavior of build in types is a really bad idea. – t.niese Mar 06 '22 at 16:24
  • @trincot so explaining the usecase will be a bit difficult over here. To give an overview of what i am trying to achieve, we have certain functionality which is giving us the delay time. The delay time remains constant for a particular session. Using that delay we need to show the time {delay} time before the current time. Something like Date.now()-delay . – Tanay Narkhede Mar 06 '22 at 16:35
  • @t.niese Can the above use case be solved without messing with the default behavior? – Tanay Narkhede Mar 06 '22 at 16:37
  • I think your biggest issue is how to call the Date constructor with multiple arguments in the case where they're provided **and** support IE 11. If that wasn't a constraint, you could do `if (arguments.length) return new oDate(...agruments)`, but in the absence of destructuring you need something like this: [*How can I construct an object using an array of values for parameters, rather than listing them out, in JavaScript?*](https://stackoverflow.com/questions/813383/how-can-i-construct-an-object-using-an-array-of-values-for-parameters-rather-th) – RobG Mar 07 '22 at 03:17

0 Answers0