Date calculations  
Author Message
Murph





PostPosted: 2003-10-24 9:57:00 Top

java-programmer, Date calculations Just a shor question concerning date calculations.
I want to take the current date, like Date currentDate = new Date(); then
subtract x number of days from it.

What I was thinking of doing was extracting the days, months and year from
currentDate and then make a method to subtract x from days if x is more
than the day in the month then I will have to rework it into the months and
possibly the year.
I was hoping there was an easier and better way of doing this.

Thanks
Murph


 
Steve W. Jackson





PostPosted: 2003-10-24 21:45:00 Top

java-programmer >> Date calculations In article <3f9887dc$email***@***.com>,
"Murph" <email***@***.com> wrote:

>:Just a shor question concerning date calculations.
>:I want to take the current date, like Date currentDate = new Date(); then
>:subtract x number of days from it.
>:
>:What I was thinking of doing was extracting the days, months and year from
>:currentDate and then make a method to subtract x from days if x is more
>:than the day in the month then I will have to rework it into the months and
>:possibly the year.
>:I was hoping there was an easier and better way of doing this.
>:
>:Thanks
>:Murph
>:
>:

Roedy Green's reply refers to a class he offers that does more and
better dates overall, so you may want to use that instead. But there's
another way if wide ranges of dates aren't your problem, which uses a
built-in class:

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY, -7);

Calendar is abstract, but getInstance() returns an instance based on
local settings which has the same values you would get with a new Date
object (see the API if you want to use other locales and/or timezones).
The add method takes the field and the signed amount to add. The result
will automatically have all related fields adjusted as required. As
Roedy would likely point out, this isn't a good solution for certain
special dates, like those before the 1600 leap year rule change, etc.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
 
Dr John Stockton





PostPosted: 2005-3-31 1:00:00 Top

java-programmer >> Date calculations
I have been, and am still, working on improving Week Number programming,
now in <URL:http://www.merlyn.demon.co.uk/weekcalc.htm>.

I have added three parameterised functions each giving non-standard
(i.e. suitable for the USA) output [Y W D] for a Date Object, and their
reverses, for
Type 1. Year Number is Calendar Year, so partial weeks are required.
Type 2. Every Week has Seven Days, so the Year Number can differ from
the Calendar Year. ISO Week Numbers are a specific case.
Type 3. Week 01 begins on a Given Date.
Please tell me of any extant sort of Week Number which is not
accommodated by those.

(That general Type 2 code is *not* optimal for ISO Week Number)


I have noted that a routine using UTC methods is much faster than a
similar routine using local methods, and have made a couple of other
routines much faster by converting them from local to UTC internally.

Probably UTC is faster because there is no need to consult, repeatedly,
the Summer Time Rules.

NOTE that much common javascript date work does not *need* local date;
for example, much calendar work can be done in UTC. The present
date/time can easily be converted into a UTC date by applying
getTimezoneOffset. The reverse takes a little more thought, for the
worst case Summer Time rules, unless the obvious brute-force method is
used.


Also, using a routine found in 'The Calendar FAQ' and another derived
from the work of Zeller, ISO 8601 Y W D can be obtained by going via
CMJD, a standard daycount - and vice versa. That's about as fast as he
best of the rest, in my MSIE4.


The names of some of my javascript functions have changed.

--
?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.
 
 
zCrow





PostPosted: 2006-1-18 3:31:00 Top

java-programmer >> Date calculations ASP

I've created a sunrise/sunset calculator on a website and want to show the
number of hours and minutes of daylight each day. The script I've created works
just fine except if the minutes are the same (ie. sunset at 4:33, sunrise at
9:33) then I get an error (33 - 33 = 0). I know there is something wrong here,
I'm just not sure what. The sunrise time is ttt, the sunset time is sss and
below is the code I'm using. In summary, the code does this: convert the
sunrise and sunset times to minutes, subtract the two, divide by 60 to get the
hours, divide by 60 to get the fraction of an hour and multiply this x 60 to
get the minutes.

The error I receive when the minutes are the same is: error '800a000d', Type
mismatch: '[string: ""]' and the line in question is dayM = Mid(dayH,2) * 60

dim dayH, dayM, totalmin
SRmin = (hour(ttt) * 60) + minute(ttt)
SSmin = (hour(sss) * 60) + minute(sss)
totalmin = (SSmin - SRmin)
dayH = totalmin / 60
'return number of hours and minutes
if Left(dayH,1) = 1 then
dayM = Mid(dayH,3) * 60
if dayM = "" then
dayM = 00
else
dayM = round(dayM)
end if
dayH = Left(dayH,2)
else
dayM = Mid(dayH,2) * 60
if dayM = "" then
dayM = 00
else
dayM = round(dayM)
end if
dayH = Left(dayH,1)
end if

response.write dayH & ":"
if dayM < 10 then
dayM = "0" & dayM
else dayM = dayM
end if
response.write dayMText