How to Deep Copy a Datetime Object

How do I deep copy a DateTime object?

$date1 = new DateTime();
$date2 = new DateTime();
$date2->add(new DateInterval('P3Y'));

Update:

If you want to copy rather than reference an existing DT object, use clone, not =.

$a = clone $b;

Do datetime objects need to be deep-copied?

Since all available types in the datetime module are documented as being immutable (right after the documentation of the classes it is stated):

Objects of these types are immutable.

you shouldn't worry about this.

Operations on a datetime instance will return a new instance thereby not affecting any other names that refer to the previous one.

You might want to take a look at the link provided by PM 2Ring that explains facts and myths about how names and values work. That should shed some light on any confusions you have about names.

How can I clone a DateTime object in C#?

DateTime is a value type (struct)

This means that the following creates a copy:

DateTime toBeClonedDateTime = DateTime.Now;
DateTime cloned = toBeClonedDateTime;

You can also safely do things like:

var dateReference = new DateTime(2018, 7, 29);
for (var h = 0; h < 24; h++) {
for (var m = 0; m < 60; m++) {
var myDateTime = dateReference.AddHours(h).AddMinutes(m);
Console.WriteLine("Now at " + myDateTime.ToShortDateString() + " " + myDateTime.ToShortTimeString());
}
}

Note how in the last example myDateTime gets declared anew in each cycle; if dateReference had been affected by AddHours() or AddMinutes(), myDateTime would've wandered off really fast – but it doesn't, because dateReference stays put:

Now at 2018-07-29 0:00
Now at 2018-07-29 0:01
Now at 2018-07-29 0:02
Now at 2018-07-29 0:03
Now at 2018-07-29 0:04
Now at 2018-07-29 0:05
Now at 2018-07-29 0:06
Now at 2018-07-29 0:07
Now at 2018-07-29 0:08
Now at 2018-07-29 0:09
...
Now at 2018-07-29 23:55
Now at 2018-07-29 23:56
Now at 2018-07-29 23:57
Now at 2018-07-29 23:58
Now at 2018-07-29 23:59

Javascript deep copy without breaking dates

This will deep copy the object, let me know if this resolves your issue:

let a = [{ date: new Date() }, { name: 'John'}];

let b = a.map(k=>({...k}));

a[1]="PETER";

console.log(a);

console.log(b);

How to clone a Date object?

Use the Date object's getTime() method, which returns the number of milliseconds since 1 January 1970 00:00:00 UTC (epoch time):

var date = new Date();
var copiedDate = new Date(date.getTime());

In Safari 4, you can also write:

var date = new Date();
var copiedDate = new Date(date);

...but I'm not sure whether this works in other browsers. (It seems to work in IE8).

Is there a way to copy Date object into another date Object without using a reference?

You could use getTime() and passing it into the Date(time) constructor. This is only required because Date is mutable.

Date original = new Date();
Date copy = new Date(original.getTime());

If you're using Java 8 try using the new java.time API which uses immutable objects. So no need to copy/clone.

DateTime Modify() php affecting previous variable

When assigning $startDt to $date the value isn't copied but referenced instead. You need to explicitly copy the object into the other variable:

# referenced
$date = $startDt;

# copied
$date = clone $startDt;


Related Topics



Leave a reply



Submit