473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Date calculations in multiple time zones

Hi,

I`m trying to do date calculations in three types of time zones: local,
GMT and specified. The issue I am facing is that I need to be able to
specify a date in the proper time zone, and I`m having a heck of a time
doing so.

I have created a form where I use drop downs do specify year, month,
date, hour, minute and seconds. When the form is loaded, the dropdowns
have to display the proper values for the current time zone type. This
code works (the one that syncs the dropdowns with a given time).

When I submit my form, the data is sent to a Java back end. The back
end will take the year, month, date, hour, minute, seconds values and
generate the proper date, using the java.util.Calendar object. That
part works fine also.

The Javascript date calculations, though, doesn't work as well. In
essence, this is what I want. Let's say it is 4:00:PM EDT and I am in
the EDT time zone. So far, so good.

If I go to my form, select "Local" as my time zone, I want my form to
show up with 16, 0, and 0 preselected in my dropdown lists.

If I choose "GMT", I want 20, 0, and 0 to be selected.

If I select "PST", I want 13, 0, and 0 to be selected.

On my form, I have some values that I can change that will do certain
interval calculations which will be posted to my Java backend. When I
do these calculations, they have to be done according to the currently
selected time zone or it will not give the proper values to the
backend.

The problem I am seeing is that all my date calculations are done using
my local time. What approach can I use to make the calculations match
the currently selected time zone?

Thanks for all help,

L

Sep 14 '05 #1
11 4607
> What approach can I use to make the calculations match
the currently selected time zone?


currently selected timezone where ??
Sep 14 '05 #2

<ld*******@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Hi,

I`m trying to do date calculations in three types of time zones: local,
GMT and specified. The issue I am facing is that I need to be able to
specify a date in the proper time zone, and I`m having a heck of a time
doing so.

I have created a form where I use drop downs do specify year, month,
date, hour, minute and seconds. When the form is loaded, the dropdowns
have to display the proper values for the current time zone type. This
code works (the one that syncs the dropdowns with a given time).

When I submit my form, the data is sent to a Java back end. The back
end will take the year, month, date, hour, minute, seconds values and
generate the proper date, using the java.util.Calendar object. That
part works fine also.

The Javascript date calculations, though, doesn't work as well. In
essence, this is what I want. Let's say it is 4:00:PM EDT and I am in
the EDT time zone. So far, so good.

If I go to my form, select "Local" as my time zone, I want my form to
show up with 16, 0, and 0 preselected in my dropdown lists.

If I choose "GMT", I want 20, 0, and 0 to be selected.

If I select "PST", I want 13, 0, and 0 to be selected.

On my form, I have some values that I can change that will do certain
interval calculations which will be posted to my Java backend. When I
do these calculations, they have to be done according to the currently
selected time zone or it will not give the proper values to the
backend.

The problem I am seeing is that all my date calculations are done using
my local time. What approach can I use to make the calculations match
the currently selected time zone?


One small hint is to keep this as simple as possible, do all calculations
and DB storage ect in ONE timezone and just use the user preference for
display purposes.

Sep 14 '05 #3
Before the page is displayed, the user must select a time zone to use.
There are three choices: GMT, local, and "pick one".

So once the page is shown, all the date and time values displayed must
match the selected timezone. A same user can display the page using a
different time zone, but she has to reload the page. IOW, you can't
select to display in GMT, load the page then, while viewing the page,
ask to display in local time. If you want to change the page view from
GMT to Local time, you have to first change the time zone to use, then
reload the page.

Is that any clearer?

L

Sep 15 '05 #4
In the backend, that's what happens. Java has pretty good Timezone
handling
capabilities. My problem is getting the Javascript display to do what
the Java
backend does: dissociate the date display from the actual time value.

In Javascript, there are two date formats: local time and UTC time. It
looks to
me like I can't deal with dates in an arbitrary time zone. It looks to
me like
dealing with arbitrary time zones requires some outside intervention
from me,
to tell Javascript how to deal with that issue.

My code looks like this:

/**
* I have a percentage box. When the user modifies that value, dates
and
* times are modified in the form.
*/
function updateDatesAndTimes(objForm, objPercent) {

pct = objPercent.value;
if (pct>100) {
pct = 100;
objPercent.value=100;
} else if (pct < 0) {
pct = 0;
objPercent.value=0;
}

// Update distance
objForm.actv_distance.value=origDistance * pct / 100;

objForm.actv_distance2.value=origDistance-objForm.actv_distance.value;
objForm.actv_odo_value.value=origOdoValue -
objForm.actv_distance2.value;

// Update dates and times
==> // actvBeginTime contains a date in milliseconds value
updateTime = new Date();
updateTime.setTime(actvBeginTime + (actvElapsedTime * pct /100));
==> // Here, the date and time values are displayed for the user. This
is where my problem
// occurs because I am not displaying in the correct time zone.
form.displayDate2.value = formatDateAsDate(updateTime,
objForm.beginDate2);
form.beginTime2.value = formatDateAsTime(updateTime);

// update duration times
form.legDuration.value = formatTime(actvElapsedTime * pct /100);
form.legDuration2.value = formatTime(actvElapsedTime -
(actvElapsedTime * pct /100));

}
/**
* Return the time of day for a given date in HH:mm::ss format.
* The value must change according to the time zone in effect. The time
* zone is not necessarily the client time zone nor GMT/UTC.
*/
function formatDateAsTime(time) {
var hour = time.getHours();
var minute = time.getMinutes();
var second = time.getSeconds();
var temp = ((hour < 10) ? "0" : "") + hour;
temp += ((minute < 10) ? ":0" : ":") + minute;
temp += ((second < 10) ? ":0" : ":") + second;
return temp;
}

L

Sep 15 '05 #5

ld*******@gmail.com wrote:
My code looks like this:


Ahh! Someon just gave me an idea: change the Locale for my request to
reflect the time zone I want to use. If I do that, I can use local
timezone all the time and the data display and calculations will be
correct.

I'll try that.

L

Sep 15 '05 #6

<ld*******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

ld*******@gmail.com wrote:
My code looks like this:


Ahh! Someon just gave me an idea: change the Locale for my request to
reflect the time zone I want to use. If I do that, I can use local
timezone all the time and the data display and calculations will be
correct.


which will lonly work if the clients machine is set up the same way...
Sep 15 '05 #7

Zoe Brown wrote:
Ahh! Someon just gave me an idea: change the Locale for my request to
reflect the time zone I want to use. If I do that, I can use local
timezone all the time and the data display and calculations will be
correct.
which will lonly work if the clients machine is set up the same way...


Yep... I'm realising that the locale isn't enough. What I'm leaning
towards is to add the timezone offset in the request and use that on my
page to display it correctly. IOW, in the function that formats dates
to display, I'll pad the time value witht the timezone offset value.From there, I'll be able to affect only the display, while all the

calculations remain correct.

I'm working on it from that angle.

L

Sep 15 '05 #8
JRS: In article <11*********************@f14g2000cwb.googlegroups. com>,
dated Wed, 14 Sep 2005 13:40:22, seen in news:comp.lang.javascript,
ld*******@gmail.com posted :
I`m trying to do date calculations in three types of time zones: local,
GMT and specified. The issue I am facing is that I need to be able to
specify a date in the proper time zone, and I`m having a heck of a time
doing so.
Please explain why reading the newsgroup FAQ has not helped you.

The Javascript date calculations, though, doesn't work as well. In
essence, this is what I want. Let's say it is 4:00:PM EDT and I am in
the EDT time zone. So far, so good.


EDT is not a time zone. Time zones are geographical areas, which exist
the whole year round.

TLAs (other than GMT and UTC) are not adequate to describe either zones
or offsets from UTC; EST has at least two meanings and BST at least
three.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 15 '05 #9
JRS: In article <11**********************@g47g2000cwa.googlegroups .com>
, dated Thu, 15 Sep 2005 06:57:28, seen in news:comp.lang.javascript,
ld*******@gmail.com <ld*******@gmail.com> posted :
Ahh! Someon just gave me an idea: change the Locale for my request to
reflect the time zone I want to use. If I do that, I can use local
timezone all the time and the data display and calculations will be
correct.


That's a really stupid idea if running in a system (such as Windows)
where many processes run at once. Any other time-dependent process may
get confused, especially appointments software.

It might be safe enough in a intranet on which all machines were DOS.

Read the Newsgroup FAQ; see below.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 15 '05 #10

Dr John Stockton wrote:
Please explain why reading the newsgroup FAQ has not helped you.


Well, for what I'm trying to do, I din't see anything that addresses
the issue. There's a lot of theory on date calculations and warnings.
But in order to do what I want to do, there isn't anything.

In my case, the only thing that seems to work correctly is to manage
everything using seconds instead of actual Date instances. I then do
the manual formatting of the data to display it correctly on the site.

In my case, displaying the TLA is not important. What is important are
the hours/minutes/seconds adjusted correctly, "correctly" being a
different beast than common usage seems to dictacte.

L

Sep 16 '05 #11
JRS: In article <11**********************@z14g2000cwz.googlegroups .com>
, dated Fri, 16 Sep 2005 12:19:28, seen in news:comp.lang.javascript,
ld*******@gmail.com <ld*******@gmail.com> posted :

Dr John Stockton wrote:
Please explain why reading the newsgroup FAQ has not helped you.
Well, for what I'm trying to do, I din't see anything that addresses
the issue. There's a lot of theory on date calculations and warnings.
But in order to do what I want to do, there isn't anything.


Well, you may be right; I have not understood just what it is that you
want to do. What you appear to say that you want to do is easy enough,
provided that the meanings of the TLAs are known.
In my case, the only thing that seems to work correctly is to manage
everything using seconds instead of actual Date instances.
Administrative work, such as displaying a calendar of whole days, can
with care be done using the localised Date methods. But for anything
dealing with real time, one should use UTC methods.
I then do
the manual formatting of the data to display it correctly on the site.
If one is writing for the WWW, as is the default assumption here, one
will always need manual formatting, since one cannot trust either that
the localisation is set to suit the user or that the browser writer uses
it correctly. If one is writing for the USA only, or for an intranet,
manual formatting may be unnecessary.
In my case, displaying the TLA is not important. What is important are
the hours/minutes/seconds adjusted correctly, "correctly" being a
different beast than common usage seems to dictacte.


To comment meaningfully on that, one would need to know both what you
consider to be correct and what you consider common usage dictates. It
may well be that neither is actually "right".

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Sep 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
5
by: Gord | last post by:
Many scripts and calendars call client side system time in order to make presentations. However, the client's time may be improperly set, if set at all, and/or the relevant time may be from...
13
by: Dr John Stockton | last post by:
Javascript date strings can have a one-letter postfix; it is taken as indicating time zone (not J, which causes NaN). // IIRC, VB accepts A & P in that location, for AM & PM. In my MS IE 4, the...
14
by: Jon Davis | last post by:
I have put my users through so much crap with this bug it is an absolute shame. I have a product that reads/writes RSS 2.0 documents, among other things. The RSS 2.0 spec mandates an en-US style...
13
by: maflatoun | last post by:
Hi, I have the following function to convert UTC time to Local time. It works perfect for GMT- (Minus) time zones however it provides incorrect results for GMT+(Plus) time zones? // Format to...
3
by: C Villalba | last post by:
Birth Date and a Purchase Date aata is being stored in sql. This information is retrieved across time zones through a .Net Web Service. When the data is viewed in a .Net client application these...
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
3
by: rharmonster | last post by:
I have a web page programmed in ASP that creates a calling list based on the user's preferences, which queries the information from a SQL 2000 database. I want the user to be able to select...
10
by: WebCM | last post by:
There is a function: http://paste.ubuntu.com/21865 It needs GMT date in YYYY-MM-DD HH:MM:SS format - in SQL: datetime. If date is the same as today, the function returns "Today". There is one...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.