Re: hot to round time to next day, next week or next month?

Tech-Archive recommends: Fix windows errors by optimizing your registry



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.

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.

Note that only times-of-day which are multiples of power-of-two
fractions of a day can be held exactly.

--
© 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.
.



Relevant Pages