Session.ClientTime and Date Logic

Suppose I have a property Session.SessionDate (As Date) and I want to assign that property the client’s local date. I do this without compilation complaints in Session.Open:

Self.SessionDate = Self.ClientTime

However, is it best to do it this way?:

Dim d As New Date(Self.ClientTime) Self.SessionDate = d

or

Self.SessionDate = New Date(Self.ClientTime)

I sometimes get confused as to when I should be using New with dates. I have assumed that if the date already exists in the method (or exists implicitly, like Session.ClientTime), I can just assign a date variable like this:

Dim d As Date = <some existing date in the method>

But sometimes I find that even if the date exists in the method, I still must do this to make it work properly:

Dim d As New Date(<some existing date in the method>)

I think I’m getting this now.

Dim d1 As New Date(Session.ClientTime) Dim d2 As Date = d1 d2.Day = d2.Day + 1

The above makes d1 and d2 share the same Date object, so in the end, they are the same Date ( 1 day after ClientTime).

Dim d1 As New Date(Session.ClientTime) Dim d2 As New Date(d1) d2.Day = d2.Day + 1

The above makes sure d1 and d2 are distinct Date objects, so d2 is 1 day later than d1.

I was having problems passing dates like this before. I couldn’t figure out when I needed to use New Date. I think this explains it.

So, back to my original concern, I think I really can use

Self.SessionDate = Self.ClientTime

instead of

Self.SessionDate = New Date(Self.ClientTime)

in Session.Open, where Session.SessionDate is a Property (As Date) in Session. Is that correct?

Why would you do that though? There’s already a property on Session that holds the browser’s date and time.

One particular reason. I want Session.SessionDate to be assigned the moment they started the Session. I refer to Session.SessionDate in various methods. So, say they start their Session at 11:50pm on 11/09/2018. In that case, I want Session.SessionDate to be 11/09/2018 during that entire Session, even if they remain in that Session more than 10 minutes. So they’re consistently working on the same day’s data during that Session. If I dynamically use Session.ClientTime in those methods, the day that refers to will change in 10 minutes.

In that case, yes, this code should work fine

Self.SessionDate = New Date(Self.ClientTime)

Is there a reason not to use

Self.SessionDate = Self.ClientTime

[quote=413589:@Ralph Alvy]Is there a reason not to use

Self.SessionDate = Self.ClientTime[/quote]
In this case, Self.SessionDate is just a reference to Self.ClientTime. So as ClientTime changes, so does SessionDate. If you want SessionDate to remain static, use Greg’s code.

Ah. Now I get it. Thanks, Tim.