Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 5 of 5 total
Thread DateTime confusion
Mon, May 22 2017 12:53 PMPermanent Link

Matthew Jones

I'm getting confused about timezones in EWB. Tim has stated "When you use the Date() function, you're getting a DateTime value that is the number of msecs since midnight, January 1, 1970, UTC."

Now, if I run the following:

var
 tmEnd : DateTime;
begin
 tmEnd := Date;
 FormatSettings.ShortTimeFormat := 'HH:mm:ss';
 DebugReport('UTC Date ' + DateToStr(tmEnd, true) + ' ' + TimeToStr(tmEnd, true));

I get an output of "UTC Date 2017-05-21 23:00:00" which given it is the 22nd, is somehow adjusting by an hour.

If I change that tmEnd := Now, I get "UTC Now 2017-05-22 16:49:26" which given I am in BST (one hour off UTC) is right, the clock showing 5.50pm as I type.

What does this imply? That Date is off by one, or that I shouldn't expect 2017-05-22 00:00:00 ?

I need to be spot on here, as users have to be able to specify a time in any time zone, and I need to end up with UTC, but also be able to edit it. Thus I'm taking the components (hour, min etc) and adding them to the current date. Only they are an hour off.

--

Matthew Jones
Mon, May 22 2017 1:07 PMPermanent Link

Matthew Jones

Matthew Jones wrote:

> Date

Am I right in thinking we don't have the source code for Date and Time? Regardless, we have the output.

function date()
{
  var $0 = new Date();
  return new Date($0.getFullYear(), $0.getMonth(), $0.getDate(), 0, 0, 0, 0).getTime();
};

function now()
{
  return new Date().getTime();
};


According to https://docs.microsoft.com/en-us/scripting/javascript/reference/date-utc-function-javascript "The difference between the Date.UTC function and the Date object constructor that accepts a date is that the Date.UTC function assumes UTC, and the Date object constructor assumes local time."
Which sort of implies that these are both getting a local, not UTC time I think. Not sure how my second time test is working therefore!

I don't think this is clearing my confusion...

Key is, I need functions guaranteed to return UTC Date and UTC Now. And to convert those into ISO strings.

--

Matthew Jones
Tue, May 23 2017 1:05 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

I need to do a technical article on this.  This topic comes up constantly and is a tough one to get one's head around.

<< Am I right in thinking we don't have the source code for Date and Time? >>

Correct, they are internal functions that are part of the RTL.  There's only a handful of such functions.

<< According to https://docs.microsoft.com/en-us/scripting/javascript/reference/date-utc-function-javascript "The difference between the Date.UTC function and the Date object constructor that accepts a date is that the Date.UTC function assumes UTC, and the Date object constructor assumes local time."
Which sort of implies that these are both getting a local, not UTC time I think. >>

You're confusing the *internal representation* of a DateTime value with the actual date-time values.  The internal representation of a DateTime value is always UTC.  That's how you can pass around such values and always have them be interpreted properly when converted to strings, etc.

However, that doesn't mean that the actual date-time value is set *based upon* UTC when calling Date(), etc.  It isn't.  Date(), Time(), and Now() will all return the date-time according to the current time zone set for the browser's OS.

<< Key is, I need functions guaranteed to return UTC Date and UTC Now. And to convert those into ISO strings. >>

  ShowMessage(DateTimeToISOStr(Now));

Tim Young
Elevate Software
www.elevatesoft.com
Wed, May 24 2017 5:29 AMPermanent Link

Matthew Jones

Tim Young [Elevate Software] wrote:

> Date(), Time(), and Now() will all return the date-time according to the current time zone set for the browser's OS.

Which is fine, but I want the UTC original.

I've worked out something that works for the moment, but a way to work only with UTC as well as local time would be good. Maybe the article would resolve everything.

--

Matthew Jones
Wed, May 24 2017 12:55 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

<< Which is fine, but I want the UTC original. >>

You *do* have the UTC original.  This returns the current UTC date-time in ISO format:

ShowMessage(DateTimeToISOStr(Now));

Tim Young
Elevate Software
www.elevatesoft.com
Image