Re: hot to round time to next day, next week or next month?
- From: "Steve Foster [SBS MVP]" <steve.foster@xxxxxxxxxxxxx>
- Date: Sun, 19 Mar 2006 18:02:35 -0800
Dr John Stockton wrote:
JRS: In article <l6ml1295la33s96mf8ndcsbqu0hq6qjv4p@xxxxxxx>, dated
Fri, 17 Mar 2006 23:54:47 remote, seen in news:microsoft.public.scriptin
g.vbscript, Apricot <yilu@xxxxxxxx> posted :
Function RoundToMonth (varDateTime)
varDateTime = DateAdd("m", 1, varDateTime)
RoundToMonth = Year(varDateTime) & "/" & Month(varDateTime) & "/01"
End Function
Function RTM(vDT)
RTM = DateSerial(Year(vDT), Month(vDT)+1, 1)
End Function
Function RoundToWeek (varDateTime)
const worldStart = "1970-1-4 0:00:00"
secs = DateDiff("s", worldStart, varDateTime)
secs = (secs \ ( 7 * 24 * 3600) + 1) * (7 * 24 * 3600)
RoundToWeek = DateAdd("s", secs, worldStart)
End Function
No need to go to seconds; work in days. I've already given an efficient
answer.
The answer you gave may be efficient, but its' function is not immediately obvious. Using the Weekday function might be slightly less efficient, but it has the advantage of being reasonably easy to understand.
Code that you can read later and immediately comprehend is often more desirable than code that is a model of efficiency.
Function RoundToDay (varDateTime)
'wscript.echo "before add " & varDateTime
varDateTime = DateAdd("d", 1, varDateTime)
'wscript.echo "after add " & varDateTime
RoundToDay = Year(varDateTime) & "/" & Month(varDateTime) & "/" &
Day(varDateTime)
'wscript.echo "After round " & varDateTime
End Function
Function RTD(vDT)
RTD = Int(vDT) + 1
End Function
Function RoundToHour (varDateTime)
Dim worldStart : worldStart = "1970/01/01 0:00:00"
hrs = DateDiff("h", worldStart, varDateTime)
RoundToHour = DateAdd("h", hrs + 1, worldStart)
End Function
Function RTH(vDT)
RTH = CDate((Int(vDT*24) + 1)/24)
End Function
It works fine on my machine.
Your functions are longer than necessary, and horribly inefficient.
Conversion between String and CDate, implicit or explicit, should be
done only when necessary.
And your solutions work on the assumption that it is safe to implicitly convert between datetime and float/int - how is that better? It happens to be true in VBScript, but relying on it rather than using available datetime functions is (IMHO) poor coding.
Note that only times-of-day which are multiples of power-of-two
fractions of a day can be held exactly.
And that's just the result of a sub-optimal datetime implementation. MS could have done better.
--
Steve Foster [SBS MVP]
---------------------------------------
MVPs do not work for Microsoft. Please reply only to the newsgroups.
.
- Follow-Ups:
- Re: hot to round time to next day, next week or next month?
- From: Dr John Stockton
- Re: hot to round time to next day, next week or next month?
- References:
- hot to round time to next day, next week or next month?
- From: Apricot
- Re: hot to round time to next day, next week or next month?
- From: Apricot
- Re: hot to round time to next day, next week or next month?
- From: Dr John Stockton
- hot to round time to next day, next week or next month?
- Prev by Date: Re: Executing DOS program with ASP/VBScript
- Next by Date: Re: hot to round time to next day, next week or next month?
- Previous by thread: Re: hot to round time to next day, next week or next month?
- Next by thread: Re: hot to round time to next day, next week or next month?
- Index(es):
Relevant Pages
|