WebDaily: Your daily source for Web Technology Tips and Tricks! -Forward this email and subscription information to a fellow developer! ---- CALL FOR AUTHORS!! ---- ---- If you'd like to write for 4GuysFromRolla.com ---- visit http://www.4GuysFromRolla.com/contribute ---- to learn more! ********************************************************************** Why I like DateSerial*********************************************************************** This article discusses why, exactly, I like DateSerial!There are some very neat and nifty functions in ASP that many people takefor granted. One such function, in my opinion, is DateSerial. To me,this is one of the neatest and most powerful functions I've run across inASP / VBScript.DateSerial, in case you don't know, is a function used to create a date.It takes three parameters: the year, month, and day of the date you wantto create. For example, say that you wanted to store the date of thesigning of the Declaration of Independence. You could write some codewhich looks like this: Dim DecIndepDate DecIdepDate = DateSerial(1776, 7, 4)This creates the date July 4, 1776 (or #7/4/1776#) and stores it in thevariable DecIndepDate.At first glance, DateSerial may not seem that useful or powerful or eventhat neat, but it really is. The power of DateSerial comes into focuswhen you are asked to get some date like the last day of the previousmonth.While it is true that you could construct a simple conditional statementto determine the current month, and see what the previous month was, andwhat was the last day, you can do this in one line with DateSerial. DateSerial(Year(Date), Month(Date), 1 - 1)This would return the first day before the first day of the current monthof the current year; in other words the last day of the month previous.Again, this may not seem like to big of a hub-bub, but it is! Imaginethat you were asked to find what day of the week it was on the first day of the previous month of the previous year. This is simple withDateSerial and DatePart.(If you are unfamiliar with DatePart, it extracts a "part" of a datevariable. You can ask for the weekday, the month, day, year, week,quarter, etc. For example, DatePart("m",DateSerial(1776,7,4)) wouldreturn the month of 7/4/1776, which is 7.)Here is the code: 'Get the weekday of the first day of the previous month of the 'previous year DatePart("w", DateSerial(Year(Date) - 1, Month(Date) - 1, 1))And the neatest, neatest part of DateSerial is that you can form reallyconfusing-sounding sentences when you use DateSerial. Here is anexample which fully illustrates that confusing neatness: DateSerial(Year(Date) - 1, Month(Date) - 2, 1 - 1)If asked what the above statement of code did, you could aptly reply: "It returns the first day before the first day of the month twomonths previous from the current month from the year previous to thecurrent year."And then your colleauges will be amazed and envious of your date-jargon.(It would be easier to explain the above line of code as returning thelast day of the month a year and three months ago from the current date,but then that wouldn't be as neat, now would it?)If you have any questions, comments, or ideas, please let me know bysimply replying to this email!Happy Programming!****************************************************************************************************************************************** To subscribe to WebDaily, point your browser to: http://www.4GuysFromRolla.com/webtech/webdaily To unsubscribe from WebDaily, reply to this email with the following subject: UNSUBSCRIBE WEBDAILY******************************************************************************************************************************************Thank you for subscribing to WebDaily!If you know someone who could benefit from a wealth of web developmenttechnology, invite them to visit http://www.4GuysFromRolla.com/new
Source Code
|