HELP on SToring dates in variables

Results 1 to 3 of 3

Thread: HELP on SToring dates in variables

  1. #1
    Join Date
    Dec 1969

    Default HELP on SToring dates in variables

    can anyone help in storing date values in variables? when i try to store one using slashes (/) i usually end up displaying a werid result. The result comes out as if the dates where divided among themselves. i.e. 12/10/2001 is equivalent to "5.9970014992503748125937031484258e-4" herlp me on this pls. any help from you guys will be highly appreciated.<BR><BR>you could reply in this message board or email me at<BR><BR>Thanks

  2. #2
    Join Date
    Dec 1969
    Los Angeles, CA

    Default VariableName = "12/10/2001" <eop>


  3. #3
    Join Date
    Dec 1969

    Default I don't agree with Akhilesh...

    ...but he&#039;ll just say "what&#039;s new?"<BR><BR>Yes, you *can* do<BR>&#060;%<BR>var = "12/10/2001"<BR>%&#062;<BR>but that creates a variable that holds a *STRING*. Yes that string happens to *look* like a date, but if you try to use it in some DateTime expressions it won&#039;t work.<BR><BR>For example, one way to add 3 days to a *TRUE* DateTime variable is<BR>&#060;%<BR>dateVar = dateVar + 3<BR>%&#062;<BR>But that won&#039;t work if you do it via<BR>&#060;%<BR>var = "12/10/2001"<BR>var = var + 3<BR>%&#062;<BR>Instead, you&#039;d end up with var holding the string "12/10/20013" ... the 3 just getting tacked on to the end of the string.<BR><BR>SO...<BR><BR>Ways to create a *true* DateTime variable:<BR><BR>(1)<BR>&#060;%<BR>dateVar = #12/10/2001#<BR>%&#062;<BR>This isn&#039;t much used in VBS, because the date *must* be a fixed constant. And usually what you are trying to do is "build up" a date. But it&#039;s actually the most efficient of all ways, if you can use it. Because the stuff inside #...# is immediately recognized by the *compiler* phase as a Date constant.<BR><BR>(2)<BR>&#060;%<BR>varDate = CDate("12/10/2001")<BR>%&#062;<BR>CDATE is a function that converts a string (or some other data types) into a DateTime value for you! <BR><BR>Again, how useful is this? Actually, it is *very* useful when trying to get DateTime values from a prior &#060;FORM&#062;:<BR>&#060;%<BR>varDate = CDate( Request("someFormFieldThatIsSupposedToBeADate") )<BR>%&#062;<BR><BR>(3)<BR>&#060;%<BR>varDate = DateSerial( 2001, 12, 10 )<BR>%&#062;<BR>Many times, this is *the* best way. It allows you to build a date from individual year, month, and day constants, variables, or full expressions! Plus, it is smart:<BR>&#060;%<BR>varDate = DateSerial( 2001, 13, -1 )<BR>%&#062;<BR>will give you December 30, 2001! DateSerial realizes that the 13th month of 2001 is actually January 2002. Good! But then it realizes that -1 means to go back *two* days from the first of January!<BR><BR>This is really useful in finding the last day of the current month:<BR>&#060;%<BR>today = Date( )<BR>lastDayOfThisMonth = DateSerial( Year(today), 1+Month(today), 0 )<BR>%&#062;<BR>Presto!<BR><BR>******************* *<BR><BR>Learn to work with DateTime variables and values in VBS. They can be really handy and easy to work with *if* you learn all the possible functions available.<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts