AAAAAHHHHH. This date "stuff" is horrible!

# Thread: AAAAAHHHHH. This date "stuff" is horrible!

1. Senior Member
Join Date
Dec 1969
Posts
417

## AAAAAHHHHH. This date "stuff" is horrible!

I can&#039;t figure this date stuff out!. Been to every online tutorial known to man!. Need to get # days in current month, and # days "left" in month and # days left in current year. Anybody with time to shoot a line of code my way?<BR>David<BR>Rookie

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## DateSerial is magic...

look it up in the list of functions in VBS.<BR><BR>Anyway:<BR><BR>numberOfDaysInThisMonth = Day( DateSerial( Year(Date), Month(Date)+1, 0 ) )<BR><BR>[Get a DATE object that refers to the zero-eth day of *next* month...which is of course last day of this month. Use the Day function to find out what number that day is. Presto.]<BR><BR>numberOfDaysLeftInThisMonth = numberOfDaysInThisMonth - Day(Date)<BR><BR>[Too easy to bother describing.]<BR><BR>lastDateThisYear = DateSerial( Year(Date), 12, 31 )<BR>numberOfDaysLeftInThisYear = lastDateThisYear - Date<BR><BR>[Again, DateSerial gets us a date of Dec 31 of this year. And, yes, you can simply subtract dates like that to get number of days between dates.]<BR><BR>You could also use DateDiff:<BR><BR>lastDateThisYear = DateSerial( Year(Date), 12, 31 )<BR>numberOfDaysLeftInThisYear = DateDiff( "d", Date, lastDateThisYear )<BR><BR>**********<BR><BR>See? Not that hard. You just have to think creatively, using the ton of date/time functions built into VBS.<BR><BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
946

## RE: AAAAAHHHHH. This date

Hi,<BR><BR>Look into this VB Script function, apply some basic math to it, and you get what you want:<BR><BR><BR>DateDiff(Interval, Date1, Date2, FirstDayofWeek, FirstWeekofYear<BR><BR>The Intervals you can choose from are:<BR>YYYY Year <BR>Q Quarter <BR>M Month <BR>Y Day Of Year <BR>D Day <BR>W WeekDay <BR>WW Week Of Year <BR>H Hour <BR>N Minute <BR>S Second <BR><BR><BR>So to found out how many days are left before 2003:<BR><BR>&#060;%=DateDiff("d", Date, "1/1/2003") %&#062; which is ~299<BR><BR>Get the point? If you need to see more of this function, check this link out: <BR><BR>http://www.devguru.com/Technologies/vbscript/quickref/datediff.html<BR><BR>Vadim C.

4. Senior Member
Join Date
Dec 1969
Posts
946

## RE: DateSerial is magic...

Hey Bill!!!<BR><BR>I haven&#039;t been on the board in 6 months!!! Remember me, the russian guy from Montreal Canada! :)<BR><BR>I&#039;ve been caught up with my new company doing Cold Fusion stuff ... I guess the one thing I can say about it... "quick" and "not mind bending" (maybe numbing??)... I miss thinking under the hood... Gonna be going to a JSP 4 day seminar soon.. looking forward to it.<BR><BR>Anyways, hope all&#039;s well w/you! Great to see you&#039;re still helping people out around here!<BR><BR>Vadim C.

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## But the thing wrong with that...

...is that he *still* needs to construct that string-ized date of "1/1/2003", meaning he has to do "1/1/" & Year(Date), assuming he wants it to depend on today&#039;s date (as I think he said he did).<BR><BR>Further, now that string form of the date has to be transformed by VBS to a DateTime value, instead, before DateDiif can work on it (same as calling CDate). My point was that, by using DateSerial, you (a) are Locale independent (e.g., does 1/3/2002 mean January 3rd or March 1st?) and (b) run just a tiniest bit faster.<BR><BR>I would choose DateSerial over *any* other method of creating dates, given a choice.<BR><BR>Besides, you didn&#039;t answer the hardest question: How do you find the number of days in the current month? DateSerial makes that trivial compared to any other solution.<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
946

## Put in my place by the greatest :(

Thanks Bill for opening my eyes! I&#039;ve never used DateSerial before, but I&#039;ll make sure to get up to speed!

7. Senior Member
Join Date
Dec 1969
Posts
96,118

I didn&#039;t mean to imply it didn&#039;t! Just explaining why I had used DateSerial in my own example.<BR><BR>

8. Senior Member
Join Date
Dec 1969
Posts
417